配置文件如下:(只包括了部分参数的设置),配置文件的名称必须为c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--连接参数-->
<property name="driverClass">com.mysql.jdbc.Driver
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false
<property name="user">root
<property name="password">root
<!--连接池参数-->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">10
<!--最大空闲时间,指定的时间内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">30
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">15
<!--最小的连接数量-->
<property name="minPoolSize">10
<!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
<property name="checkoutTimeout">3000
<named-config name="mySource">
<property name="driverClass">com.mysql.jdbc.Driver
<property name="jdbcUrl">jdbc:mysql://localhost:3306/bookstore
<property name="user">root
<property name="password">xxxx
<property name="initialPoolSize">10
<property name="maxIdleTime">30
<property name="maxPoolSize">100
<property name="minPoolSize">10
JAVA代码演示
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Demo1 {
public static void main(String[] args)throws SQLException {
//创建数据库连接池对象
ComboPooledDataSource comboPooledDataSource =new ComboPooledDataSource();//使用连接池的方法
//获取连接对象
Connection connection = comboPooledDataSource.getConnection();
//打印
System.out.println(connection);
}
}