方法一:直接连接
package demo03; import org.junit.jupiter.api.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; public class testGetConnection { //方法一 @Test public void test() throws Exception{ ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test" ); cpds.setUser("root"); cpds.setPassword("root"); cpds.setInitialPoolSize(10); java.sql.Connection con = cpds.getConnection(); System.out.println(con); } }
方法二:使用xml配置文件
package demo03; import org.junit.jupiter.api.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; public class testGetConnection { //方法二:获取XML配置文件 @Test public void test_() throws Exception { ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp"); java.sql.Connection coon = cpds.getConnection(); System.out.println(coon); } }
c3p0-config.xml文件:
<?xml version="1.0" coding="UTF-8"?> <c3p0-config> <!-- This app is massive! --> <named-config name="intergalactoApp"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:nysql://localhost:3306/testdb</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">100</property> <!-- intergalactoApp adopts a different approach to configuring statement caching --> <property name="maxStatements">0</property> <property name="maxStatementsPerConnection">5</property> </named-config> </c3p0-config>