配置文件 必須以c3p0-config.xml 命名mysql
放置src目錄中web
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///db1</property> <property name="user">root</property> <property name="password"> </property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="itheima"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///web08</property> <property name="user">root</property> <property name="password">root</property> </named-config> </c3p0-config>
C3P0測試類sql
public class Test_c3p0 { @Test public void test2() { Connection conn = null; PreparedStatement pstmt = null; ComboPooledDataSource dataSource = new ComboPooledDataSource(); try { // 1.獲取鏈接 conn = dataSource.getConnection(); // 2.編寫sql語句 String sql = "insert t1 (id,name) value (?,?)"; // 3.獲取執行sql語句對象 pstmt = conn.prepareStatement(sql); // 4.設置參數 pstmt.setInt(1, 7); pstmt.setString(2, "wuwuww"); // 5.執行刪除操做 int row = pstmt.executeUpdate(); if (row > 0) { System.out.println("刪除成功!"); } else { System.out.println("刪除失敗!"); } } catch (Exception e) { throw new RuntimeException(e); } finally { // 6.釋放資源 JBDC_V2.release(conn, pstmt, null); } } }