c3p0连接池是开源的使用较为广泛的连接池之一,它的使用相对而言比Druid更加灵活一点。
jar的下载路径:
https://sourceforge.net/projects/c3p0/
下载下来之后是一个项目,找到lib目录下的该文件就可以了。
使用起来很简单:
下面是我封装好的c3p0的工具类,供大家参考下。
public class JDBCUtils2_c3p0 {
//一个项目只需要一个连接池即可,这样就没必要每个用户来访问都需要创建连接池。
//
private static final ComboPooledDataSource cpds = new ComboPooledDataSource();
//开源的c3p0连接池在创建对象的同时会访问当前资源下的一个c3p0_config.xml文件
//我们只需要把连接所需要的参数配置到该文件下,把文件放置到src目录下即可访问到。
public static Connection getConnection() throws SQLException {
return cpds.getConnection();
}
public static DataSource getDataSource(){
return cpds;
}
}
c3p0-config.xml
此文件用来配置数据库属性,放置到项目的src目录下,c3p0在创建连接池对象的同时会去加载此文件。
<?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/sysmanage?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&rewriteBatchedStatements=true</property>
<property name="user">root</property>
<property name="password">******</property>
</default-config>
</c3p0-config>
工具就这几行代码,然后就可以使用了,下面附上c3p0开源连接池的官方使用手册。
https://www.mchange.com/projects/c3p0/#using_c3p0