1.需要用到的jar包
c3p0-0.9.1.2.jar
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
mysql-connector-java-5.1.43-bin.jar
2.创建xml文件,设置连接数据库所需参数
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day38?useSSL=true</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</default-config>
</c3p0-config>
3.代码实现
public class Demo {
@Test
//使用XML配置文件的方式,创建C3P0连接池
public void testXML() throws Exception {
//创建c3p0核心类
//创建当前核心类,就会自动加载在src下的c3p0-config.xml
ComboPooledDataSource dataSource = new ComboPooledDataSource();
PreparedStatement pstmt = null;
Connection conn = dataSource.getConnection();
for (int i = 0; i < 10; i++) {
String sql = "insert into student(name) values(?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "逗比");
pstmt.executeUpdate();
}
pstmt.close();
conn.close();
}
}