Post Code First
1 Public classSqlc3p0utils {2 Private StaticCombopooleddatasource DataSource;3 Static{4ResourceBundle bundle=resourcebundle.getbundle ("db");//Get Profile Object5DataSource =NewCombopooleddatasource ();//Get Connection Pool object6 //Configure according to the Profile team connection pool7 Try {8Datasource.setdriverclass (bundle.getstring ("Driverclass"));9}Catch(propertyvetoexception e) {Ten //TODO auto-generated Catch block One e.printstacktrace (); ASystem.out.print ("Driver not Found"); - } -Datasource.setjdbcurl (bundle.getstring ("url")); theDatasource.setuser (bundle.getstring ("username")); -Datasource.setpassword (bundle.getstring ("Password")); -Datasource.setminpoolsize (10);//Connection Pool Minimum number of connections -Datasource.setinitialpoolsize (20);//Connection Pool creation time number of connections +Datasource.setmaxpoolsize (80);//Connection Pool Maximum number of connections -Datasource.setacquireincrement (10);//the number of connections created when the link runs out +Datasource.setnumhelperthreads (5);//multi-threaded execution for improved performance A } at /** - * - * @returnreturn a link - */ - Public StaticConnection getconnection () { - Try { in returndatasource.getconnection (); -}Catch(SQLException e) { to //TODO auto-generated Catch block + e.printstacktrace (); -SYSTEM.OUT.PRINTLN ("No link returned"); the return NULL; * } $ }Panax Notoginseng - /** the * @seeClose Resource + * @paramRS A * @paramSTM the * @paramConn + */ - Public Static voidreleast (ResultSet rs,statement stm,connection conn) { $ if(rs!=NULL){ $ Try { - Rs.close (); -}Catch(SQLException e) { the - E.printstacktrace (); Wuyi}finally{ thers=NULL; - } Wu } - About if(stm!=NULL){ $ Try { - Stm.close (); -}Catch(SQLException e) { - A E.printstacktrace (); +}finally{ thestm=NULL; - } $ } the if(conn!=NULL){ the Try { the conn.close (); theSYSTEM.OUT.PRINTLN ("conn Yiguanbi"); -}Catch(SQLException e) { in the E.printstacktrace (); the}finally{ Aboutconn=NULL; the } the } the } +}
The program first executes a static code block to load the configuration file
Get Profile Object
ResourceBundle bundle=resourcebundle.getbundle ("db"); // Get Profile Object
Declaring a connection pool object (outside of a static code block)
Private Static Combopooleddatasource DataSource;
Get Connection Pool Object
New Combopooleddatasource (); // Get Connection Pool object
Get configuration file Contents
bundle.getstring ("url")
Setting up a connection pool
1Datasource.setjdbcurl (bundle.getstring ("url")); 2Datasource.setuser (bundle.getstring ("username")); 3Datasource.setpassword (bundle.getstring ("Password")); 4Datasource.setminpoolsize (10);//Connection Pool Minimum number of connections5Datasource.setinitialpoolsize (20);//Connection Pool creation time number of connections6Datasource.setmaxpoolsize (80);//Connection Pool Maximum number of connections7Datasource.setacquireincrement (10);//the number of connections created when the link runs out8Datasource.setnumhelperthreads (5);//multi-threaded execution for improved performance
The above static code block executes when the class is loaded, and executes only once, avoiding the error of creating the connection pool each time the object is acquired, preventing the problem of too many links accumulating
Get Links:
1 /**2 * 3 * @returnreturn a link4 */5 Public StaticConnection getconnection () {6 Try {7 returndatasource.getconnection ();8}Catch(SQLException e) {9 //TODO auto-generated Catch blockTen e.printstacktrace (); OneSYSTEM.OUT.PRINTLN ("No link returned"); A return NULL; - } -}
Close the resource and put back the link:
1 /**2 * @seeClose Resource3 * @paramRS4 * @paramSTM5 * @paramConn6 */7 Public Static voidreleast (ResultSet rs,statement stm,connection conn) {8 if(rs!=NULL){ 9 Try { Ten Rs.close (); One}Catch(SQLException e) { A - E.printstacktrace (); -}finally{ thers=NULL; - } - } - + if(stm!=NULL){ - Try { + Stm.close (); A}Catch(SQLException e) { at - E.printstacktrace (); -}finally{ -stm=NULL; - } - } in - if(conn!=NULL){ to Try { + conn.close (); -SYSTEM.OUT.PRINTLN ("conn Yiguanbi"); the}Catch(SQLException e) { * $ E.printstacktrace (); Panax Notoginseng}finally{ -conn=NULL; the } + } A } the
Jdbcutils, a gadget that operates a relational database