加入jar包
上代码
配置文件
<c3p0-config>
<!-- This app is massive! -->
<named-config name="myconfig">
<!-- 设置常用的属性 url 用户 密码 驱动 还是set后小写第一个字母 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/student?useSSL=false</property>
<property name="user">root</property>
<!-- <property name="password">50</property> -->>
<!-- 初始化连接池 一次增加的数量, 初始化数量 最小的连接数 最大的连接数 -->>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">50</property>
<!-- 连接池维护的 statement数量 和每个链接最大使用的statement的数量 -->
<property name="maxStatements">20</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
package top.demo.test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.*;
public class TestC3p0 {
public static void main(String argv[]) throws PropertyVetoException, SQLException {
test2();
}
public static void test1() throws PropertyVetoException, SQLException{
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:mysql://localhost/student?useSSL=false" );
cpds.setUser("root");
cpds.setPassword("");
//设置池 初始化大小
cpds.setInitialPoolSize(5);
Connection con =cpds.getConnection();
System.out.println(con);
}
//c3p0可以使用配置文件加载配置 但是推荐使用xml加载配置
public static void test2() throws SQLException {
//在src下建立c3p0-config.xml 文件 文件名必须这样写
//有了xml配置文件 new 的使用写 在 <named-config name="myconfig"> 填写的name即可
ComboPooledDataSource dataSources= new ComboPooledDataSource("myconfig");
Connection con =dataSources.getConnection();
System.out.println(con);
}
}