基本测试
1、导包
c3p0
c3p0-0.9.2-pre5.jar
c3p0依赖
mchange-commons-java-0.2.3.jar
mysql驱动
mysql-connector-java-5.1.37-bin.jar
2、测试
package cn.ant.Day18;import java.sql.Connection;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Demo01 {public static void main(String[] args) throws Exception {// TODO Auto-generated method stub//1、创建连接池(获取数据源)ComboPooledDataSource dataSource=new ComboPooledDataSource();//2、基本参数设置dataSource.setDriverClass("com.mysql.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");dataSource.setUser("root");dataSource.setPassword("123456");//3、设置其他参数dataSource.setInitialPoolSize(5);//初始的连接数dataSource.setMinPoolSize(3);//最小的连接dataSource.setMaxPoolSize(10);//最大的连接dataSource.setAcquireIncrement(2);//设置增长的连接数dataSource.setMaxIdleTime(30);//以秒为单位--最大空闲时间-当连接空闲了30秒就自动回收//4、从连接池中获取连接对象Connection conn=dataSource.getConnection();System.out.println(conn);}}
3、结果
配置文件使用c3p0
1、导包(3个)
2、编写配置文件
<c3p0-config><!-- 默认配置,如果没有指定则使用这个配置 只能有一个默认配置 --><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb</property><property name="user">root</property><property name="password">123456</property><property name="checkoutTimeout">30000</property><property name="idleConnectionTestPeriod">30</property><property name="initialPoolSize">10</property><property name="maxIdleTime">3</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property><user-overrides user="test-user"><property name="maxPoolSize">10</property><property name="minPoolSize">1</property><property name="maxStatements">0</property></user-overrides></default-config><!-- 命名的配置 可以写多个 --><named-config name="ant"><!-- 连接数据库的4项基本参数 --><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb</property><property name="user">root</property><property name="password">123456</property><!-- 如果池中数据连接不够时一次增长多少个 --><property name="acquireIncrement">5</property><!-- 初始化连接数 --><property name="initialPoolSize">20</property><!-- 最小连接受 --><property name="minPoolSize">10</property><!-- 最大连接数 --><property name="maxPoolSize">40</property><!-- -JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量 --><property name="maxStatements">0</property><!-- 连接池内单个连接所拥有的最大缓存statements数 --><property name="maxStatementsPerConnection">5</property></named-config></c3p0-config>
3、测试(使用默认配置default-config)
package cn.ant.Day18;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 Connection getConnection(){try {return dataSource.getConnection();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}//获取连接池对象 为了创建连接池的数据服务public static DataSource getSource(){return dataSource;}//测试public static void main(String[] args) {Connection conn=getConnection();System.out.println(conn);}}

4、测试,使用自定义的配置
package cn.ant.Day18;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("ant");//封装一个方法获取连接对象public static Connection getConnection(){try {return dataSource.getConnection();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}//获取连接池对象 为了创建连接池的数据服务public static DataSource getSource(){return dataSource;}//测试public static void main(String[] args) {Connection conn=getConnection();System.out.println(conn);}}

注意
- c3p0会自动加载c3p0配置文件,但是要求是c3p0的配置文件必须放在src目录下
- 使用自定义配置的时候,需要给ComboPooledDataSource传递一个参数(
)中的ant,即名字
