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文件,设置连接数据库所需参数
jdbc:mysql://localhost:3306/day38?useSSL=true
com.mysql.jdbc.Driver
root
123456
3
6
1000
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();
}
}