First, the introduction of JAR package
Second, Java code
1 @Test2 Public voidTestxml ()throwsException {3 //creating the C3P0 Connection Pool core tool class4 //automatic loading src under c3p0 profile "C3p0-config.xml"5Combopooleddatasource DataSource =NewCombopooleddatasource ();//Use the default configuration6PreparedStatement pstmt =NULL;7 8 //Get Connections9Connection con =datasource.getconnection ();Ten for(intI=1; i<11;i++){ OneString sql = "INSERT into employee (EMPNAME,DEPT_ID) VALUES (?,?)"; A //Perform the update -PSTMT =con.preparestatement (SQL); -Pstmt.setstring (1, "Rose" +i); thePstmt.setint (2, 1); - pstmt.executeupdate (); - } - pstmt.close (); + //Close - con.close (); + A}
Iii. configuration Files (c3p0-config.xml)
1 <C3p0-config>2 <Default-config>3 < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/jdbc_demo4 </ Property>5 < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>6 < Propertyname= "User">Root</ Property>7 < Propertyname= "Password">Root</ Property>8 < Propertyname= "Initialpoolsize">3</ Property>9 < Propertyname= "Maxpoolsize">6</ Property>Ten < Propertyname= "MaxIdleTime">1000</ Property> One </Default-config> A - - <Named-configname= "Oracle_config"> the < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/jdbc_demo</ Property> - < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property> - < Propertyname= "User">Root</ Property> - < Propertyname= "Password">Root</ Property> + < Propertyname= "Initialpoolsize">3</ Property> - < Propertyname= "Maxpoolsize">6</ Property> + < Propertyname= "MaxIdleTime">1000</ Property> A </Named-config> at - - </C3p0-config>
Iv. jdbcutils (using connection pooling)
1 Public classJdbcutil {2 3 //Initializing the connection pool4 Private StaticDataSource DataSource;5 6 //Static code block initializes the connection pool resource and executes only once7 Static{8DataSource =NewCombopooleddatasource ();9 }Ten One /** A * Return Connection object - */ - Public StaticConnection getconnection () { the Try { - returndatasource.getconnection (); -}Catch(SQLException e) { - Throw NewRuntimeException (e); + } - } + A /** at * Close - */ - Public Static voidCloseAll (Connection con, Statement stmt, ResultSet rs) { - Try { - if(rs! =NULL) { -Rs.close ();//Fast Exception Capture Alt + SHIFT + Z inrs =NULL;//recommend recycling resources during garbage collection - } to if(stmt! =NULL) { + stmt.close (); -stmt =NULL; the } * if(Con! =NULL&&!con.isclosed ()) { $ con.close ();Panax NotoginsengCon =NULL; - } the}Catch(SQLException e) { + Throw NewRuntimeException (e); A } the } +}
C3P0 Connection Pool & Jdbcutils