
官网已经炸了
https://mvnrepository.com/
c3p0-0.9.1.2.jar
package com.atguigu4.connection;import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.SQLException;import org.junit.Test;import com.mchange.v2.c3p0.ComboPooledDataSource;import com.mchange.v2.c3p0.DataSources;public class C3P0Test {//方式一:@Testpublic void testGetConnection() throws Exception{//获取c3p0数据库连接池ComboPooledDataSource cpds = new ComboPooledDataSource();cpds.setDriverClass( "com.mysql.jdbc.Driver" );cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test" );cpds.setUser("root");cpds.setPassword("abc123");//通过设置相关的参数,对数据库连接池进行管理://设置初始时数据库连接池中的连接数cpds.setInitialPoolSize(10);Connection conn = cpds.getConnection();System.out.println(conn);//销毁c3p0数据库连接池// DataSources.destroy( cpds );}//方式二:使用配置文件@Testpublic void testGetConnection1() throws SQLException{ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0");Connection conn = cpds.getConnection();System.out.println(conn);}}
src/c3p0-config.xml
xml文件名不要改
<?xml version="1.0" encoding="UTF-8"?><c3p0-config><named-config name="hellc3p0"><!-- 提供获取连接的4个基本信息 --><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///test</property><property name="user">root</property><property name="password">abc123</property><!-- 进行数据库连接池管理的基本信息 --><!-- 当数据库连接池中的连接数不够时,c3p0一次性向数据库服务器申请的连接数 --><property name="acquireIncrement">5</property><!-- c3p0数据库连接池中初始化时的连接数 --><property name="initialPoolSize">10</property><!-- c3p0数据库连接池维护的最少连接数 --><property name="minPoolSize">10</property><!-- c3p0数据库连接池维护的最多的连接数 --><property name="maxPoolSize">100</property><!-- c3p0数据库连接池最多维护的Statement的个数 --><property name="maxStatements">50</property><!-- 每个连接中可以最多使用的Statement的个数 --><property name="maxStatementsPerConnection">2</property></named-config></c3p0-config>
放在JDBCutil.java
不会关掉连接池
//数据库连接池只需提供一个即可。private static ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0");public static Connection getConnection1() throws SQLException{Connection conn = cpds.getConnection();return conn;}