1. 程式人生 > >C3P0 與 DBCP連線池使用

C3P0 與 DBCP連線池使用

 一:C3P0

使用需要引入jar包,配置c3p0-config.xml

 

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///typ?useUnicode=true&amp;characterEncoding=utf-8</property>
		<property name="user">root</property>
		<property name="password">admin</property>
		<property name="initialPoolSize">5</property>
		<property name="maxPoolSize">20</property>
	</default-config>

	<named-config name="ityp">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///typ?useUnicode=true&amp;characterEncoding=utf-8</property>
		<property name="user">root</property>
		<property name="password">admin</property>
	</named-config>


</c3p0-config>

 c3p0提供核心工具類,使用連線池必須建立該類的例項物件

package WEB10_JDBC_Utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
	private static ComboPooledDataSource dataSource= new ComboPooledDataSource();
	
	public static DataSource getDataSource() {
		return dataSource;
	}
	public static Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
}

 TestC3P0測試類

	@Test
	public void add1() {
		Connection conn = null;
		PreparedStatement pstmt= null;
		try {
			conn = C3P0Utils.getConnection();
			String sql = "insert into user1 values(null,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "李四");
			pstmt.setString(2, "123");
			int row =pstmt.executeUpdate();
			if(row>0) {
				System.out.println("yes");
			}else {
				System.out.println("no");
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

 

 二:DBCP

需要引入jar包,配置xx.properties

xx.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/typ?useUnicode=true&characterEncoding=utf8
username=root
password=admin

工具類

package WEB10_JDBC_Utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;


public class DBCPUtils {
	private static DataSource dataSource;
	static {
		try {
			InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties");
			Properties props = new Properties();
			props.load(is);
			dataSource = BasicDataSourceFactory.createDataSource(props);
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}
	
	public static DataSource getDataSource() {
		return dataSource;
	}
	public static Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
}

TestDBCP 測試類

package JDBCTest;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import WEB10_JDBC_Utils.DBCPUtils;

public class DBCPTest {
	
	@Test
	public void add() {
		Connection conn = null;
		PreparedStatement pstmt =null;
		try {
			conn = DBCPUtils.getConnection();
			String sql = "insert into user1 values(null,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "李虎");
			pstmt.setString(2, "123");
			int row =pstmt.executeUpdate();
			if(row>0) {
				System.out.println("yes");
			}else {
				System.out.println("no");
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}
}

相比較而言,DBCP需要載入配置檔案

 

C3P0插入mysql資料庫的時候,插入中文,出現 “??” 問題

c3p0-config.xml

jdbcurl 處:

jdbc:mysql:///typ?useUnicode=true&amp;characterEncoding=utf-8

OK!!

XML檔案 &amp; 代表&