XML配置文件:
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 默认配置,当使用ComboPooledDataSource无参构造器时,使用的就是这个配置 --> <default-config> <!-- 基本配置 --> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">123</property> <!-- 每次增量,当需要创建Connection对象时,一次创建几个 --> <property name="acquireIncrement">3</property> <!-- 当创建池对象后,池中应该有几个Connection对象 --> <property name="initialPoolSize">10</property> <!-- 池中最少Connection个数,如果少于这个值,就会创建Connection --> <property name="minPoolSize">2</property> <!-- 池中最大连接个数 --> <property name="maxPoolSize">10</property> </default-config> <!-- 命名配置,new ComboPooledDataSource("oralce-config")时,使用的就是这个配置 --> <named-config name="oracle-config"> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">123</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>
配置路径:
放在项目下的src中,名字为c3p0-config.xml
java 创建连接池:
package cn.itcast.demo1; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * c3p0 * @author cxf * */ public class Demo1 { /** * 代码配置 * @throws PropertyVetoException * @throws SQLException */ @Test public void fun1() throws PropertyVetoException, SQLException { // 创建连接池对象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 对池进行四大参数的配置 dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb3"); dataSource.setUser("root"); dataSource.setPassword("123"); // 池配置 dataSource.setAcquireIncrement(5); dataSource.setInitialPoolSize(20); dataSource.setMinPoolSize(2); dataSource.setMaxPoolSize(50); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } /** * 配置文件的默认配置 * @throws SQLException */ @Test public void fun2() throws SQLException{ /** * 在创建连接池对象时,这个对象就会自动加载配置文件!不用我们来指定 */ ComboPooledDataSource dataSource = new ComboPooledDataSource(); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } /** * 使用命名配置信息 * @throws SQLException */ @Test public void fun3() throws SQLException{ /** * 构造器的参数指定命名配置元素的名称! * <named-config name="oracle-config"> */ ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle-config"); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } }
工具类调用数据库连接池:
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtils { // 配置文件的默认配置!要求你必须给出c3p0-config.xml!!! private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); /** * 使用连接池返回一个连接对象 * @return * @throws SQLException */ public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } /** * 返回连接池对象! * @return */ public static DataSource getDataSource() { return dataSource; } }