c3p0-config.xml 在src目录下
<c3p0-config> <default-config> <property name="jdbcUrl">jdbc:mysql://localhost:3306/day20 </property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">3</property> <property name="maxPoolSize">6</property> <property name="maxIdleTime">1000</property> </default-config> </c3p0-config>
@Test //2. XML配置方式,使用C3P0连接池管理连接 public void testXML() throws Exception { // 创建c3p0连接池核心工具类 // 自动加载src下c3p0的配置文件【c3p0-config.xml】 ComboPooledDataSource dataSource = new ComboPooledDataSource();// 使用默认的配置 PreparedStatement pstmt = null; // 获取连接 Connection con = dataSource.getConnection(); for (int i=1; i<11;i++){ String sql = "insert into employee(empName,dept_id) values(?,?)"; // 执行更新 pstmt = con.prepareStatement(sql); pstmt.setString(1, "Rose" + i); pstmt.setInt(2, 1); pstmt.executeUpdate(); } pstmt.close(); // 关闭 con.close(); }