1.在pom.xml文件中导入jar包同时不要忘记jdbc包,否则会报错
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
2.在src目录下创建c3p0-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--使用默认的配置读取连接池对象-->
<default-config>
<!--连接参数-->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai</property>
<property name="user">root</property>
<property name="password">root</property>
<!--连接池参数-->
<!--初始化申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--超时时间-->
<property name="checkoutTimeout">3000</property>
<property name="maxIdleTime">30</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="mysql">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
<named-config name="oracle">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
3.写一个类测试下
package c3p0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Demo1 {
public static void main(String[] args) throws SQLException {
//1.创建数据库连接池对象
DataSource ds = new ComboPooledDataSource();
//2.获取连接对象
Connection conn = ds.getConnection();
//3.打印
System.out.println(conn);
}
}
输出下面语句表示成功!其中红色的不是报错,而是日志
4.访问其他配置文件
package c3p0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Demo2 {
public static void main(String[] args) throws SQLException {
//1.获取DataSource 即连接池对象,使用默认配置
DataSource ds = new ComboPooledDataSource();
//2.获取连接对象
/*for (int i = 1; i <=11 ; i++) {//报错,因为池子最大连接数为10
Connection conn = ds.getConnection();
System.out.println(i+":"+conn);
}*/
for (int i = 1; i <=11 ; i++) {//正确,因为归还了
Connection conn = ds.getConnection();
System.out.println(i+":"+conn);
if (i==5){
conn.close();//归还连接到连接池
}
}
System.out.println("----------");
test();
}
public static void test() throws SQLException {
//1.1获取DataSource,使用指定名称配置
DataSource ds = new ComboPooledDataSource("mysql");
for (int i = 1; i <=60 ; i++) {//不报错,因为新配置的最大连接数很大
Connection conn = ds.getConnection();
System.out.println(i+":"+conn);
}
}
}