import java.beans.PropertyVetoException;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class TestC3p0 {
//在c3p0-config.xml配置文件中定义连接池基本配置
@Test
public void test2(){
DataSource ds=new ComboPooledDataSource("helloc3p0");
try {
System.out.println(ds.getConnection());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//用代码配置连接池配置
@Test
public void test1(){
ComboPooledDataSource cpds=new ComboPooledDataSource();
cpds.setUser("root");
cpds.setPassword("");
cpds.setJdbcUrl("jdbc:mysql:///jsd");
try {
cpds.setDriverClass("com.mysql.jdbc.Driver");
System.out.println("获取连接 "+cpds.getConnection());
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Test2中,c3p0-config.xml的配置文件如下(在src下):
<c3p0-config>
<named-config name="helloc3p0">
<!-- 指定c3p0的基本配置 -->
<property name="user">root</property>
<property name="password"></property>
<property name="jdbcUrl">jdbc:mysql:///jsd</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 若数据库中连接数不足时,一次性向数据库服务器申请多少 个连接数 -->
<property name="acquireIncrement">5</property>
<!-- 初始化连接池,连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 指定最小连接数:在数据库连接池中保存的最少的空闲的连接数量 -->
<property name="minPoolSize">55</property>
<!-- 指定最大连接数,同一时刻可以向数据库申请的连接数 -->
<property name="maxPoolSize">10</property>
<!-- c3p0数据库可以连接池可以维护的Statement个数 -->
<property name="maxStatements">0</property>
<!-- c3p0数据库连接池可以维护的PreparedStatement个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>