一:添加依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency>
二:添加配置默认二种方式配置文件,XML。在源码com.mchange.v2.c3p0.cfg包下
2.1:c3p0.properties
driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password= initialPoolSize=10 maxIdleTime=50 maxPoolSize=20 minPoolSize=5
2.2:c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/dev</property> <property name="user">root</property> <property name="password"></property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </default-config> <named-config name="mySource"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property> <property name="user">root</property> <property name="password"></property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </named-config> </c3p0-config>
三:配置数据源
package com.jachs.c3p0.config; import java.beans.PropertyVetoException; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import com.mchange.v2.c3p0.ComboPooledDataSource; import com.mchange.v2.c3p0.cfg.C3P0ConfigUtils; /*** * * @author zhanchaohan * */ public class C3p0Utill { /*** * 加载Properties方式初始化数据源 * @return * @throws FileNotFoundException * @throws IOException * @throws PropertyVetoException * @see C3P0ConfigUtils */ public ComboPooledDataSource initProperties() throws FileNotFoundException, IOException, PropertyVetoException { Properties properties=C3P0ConfigUtils.findResourceProperties(); ComboPooledDataSource cpds=new ComboPooledDataSource(); // cpds.setProperties(properties);//不起作用 cpds.setDriverClass(properties.getProperty("driverClassName")); cpds.setJdbcUrl(properties.getProperty("url")); cpds.setUser(properties.getProperty("username")); cpds.setPassword(properties.getProperty("password")); cpds.setInitialPoolSize(Integer.parseInt(properties.getProperty("initialPoolSize"))); cpds.setMaxIdleTime(Integer.parseInt(properties.getProperty("maxIdleTime"))); cpds.setMaxPoolSize(Integer.parseInt(properties.getProperty("maxPoolSize"))); cpds.setMinPoolSize(Integer.parseInt(properties.getProperty("minPoolSize"))); return cpds; } /*** * * @return * @see C3P0ConfigXmlUtils */ public ComboPooledDataSource initXML() { // ComboPooledDataSource cpds=new ComboPooledDataSource("mySource"); ComboPooledDataSource cpds=new ComboPooledDataSource();//不给参数使用默认数据源 return cpds; } }
四:测试
package com.jachs.c3p0.config; import java.beans.PropertyVetoException; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Before; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; /*** * * @author zhanchaohan * */ public class C3P0Test { private C3p0Utill c3p0Utill = new C3p0Utill(); ComboPooledDataSource prCpds; ComboPooledDataSource xmlCpds; @Before public void init() throws FileNotFoundException, IOException, PropertyVetoException { prCpds = c3p0Utill.initProperties(); xmlCpds = c3p0Utill.initXML(); } @Test public void tetcPrCpds() throws SQLException { Connection connection = prCpds.getConnection(); ResultSet resultSet = connection.prepareStatement("show tables").executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString(1)); } } @Test public void tetcxmlCpds() throws SQLException { Connection connection = xmlCpds.getConnection(); ResultSet resultSet = connection.prepareStatement("show tables").executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString(1)); } } }