package cn.code.demo; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; /* * c3p0 * c3p0配置文件要求: * 文件名称必须是 c3p0-config.xml * 位置必须在src下 * */ public class C3P0Demo1 { @Test public void fun1() throws PropertyVetoException, SQLException{ ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1"); dataSource.setUser("root"); dataSource.setPassword("123"); //池配置 // dataSource.setInitialPoolSize(20); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } @Test public void fun2() throws SQLException{ ComboPooledDataSource dataSource = new ComboPooledDataSource(); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } }
//配置文件
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 默认配置信息 --> <default-config> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">123</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> <!-- 自定义配置信息(模拟oracle) --> <named-config name="oracle-config"> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">123</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>
时间: 2024-10-24 20:09:10