基本使用方法
@Test
public void demo() throws PropertyVetoException, SQLException{
ComboPooledDataSource dataSource=new ComboPooledDataSource();
//设置四大参数的配置
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/数据库的名称");
dataSource.setUser("root");
dataSource.setPassword("密码");
//对缓冲池进行设置
// dataSource.setAcquireIncrement(6);
// dataSource.setInitialPoolSize(10);
// dataSource.setMinPoolSize(5);
// dataSource.setMaxPoolSize(40);
Connection con=dataSource.getConnection();
//打印查看连接对象
System.out.println(con);//打印的结果为:com.mchange.v2.c3p0.impl.NewProxyConnection@5090d8ea
con.close();
配置文件
文件名必须叫 c3p0-config.xml
文件位置必须在src下
配置文件如下
<?xml version="1.0"encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/数据库名称</property>
<property name="user">root</property>
<property name="password">密码</property>
<property name="acquireIncrement">2</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
//代码实现
ComboPooledDataSource dataSource=new ComboPooledDataSource();
Connection con=dataSource.getConnection();//直接得到该连接对象
System.out.println(con);//com.mchange.v2.c3p0.impl.NewProxyConnection@565bb966
con.close();
}