java-c3p0 database connection pool

Configuration file must be named c3p0-config.xml

Placement of the src directory

<?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 test class

public  class Test_c3p0 { 

    @Test 
    public  void test2 () { 
        Connection Conn = null ; 
        the PreparedStatement pstmt = null ; 
        ComboPooledDataSource the dataSource = new new ComboPooledDataSource (); 

        the try {
             // 1. Get connected 
            Conn = dataSource.getConnection ();
             // 2. write sql statement 
            String sql = "INSERT T1 (the above mentioned id, name) value (,??)" ;
             // 3. obtain execute sql statement object 
            pstmt =conn.prepareStatement (SQL);
             // 4. setting parameters 
            pstmt.setInt (. 1,. 7 ); 
            pstmt.setString ( 2, "wuwuww" );
             // 5. The delete operation 
            int Row = pstmt.executeUpdate ();
             IF (Row> 0 ) { 
                System.out.println ( "deleted successfully!" ); 
            } the else { 
                System.out.println ( "deletion failed!" ); 
            } 
        } the catch (Exception E) {
             the throw  new new a RuntimeException (E); 
        }the finally {
             // 6. The release resources 
            JBDC_V2.release (Conn, pstmt, null ); 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/zhuzhiwei-2019/p/11300579.html