One, the required jar package
Commons-dbcp
Commons-pool
Second, Java code
1 @Test2 Public voidTestprop () {3Connection con =NULL;4 Try {5 //load the prop configuration file6Properties prop =NewProperties ();7 //Get file Stream8InputStream instream = app_dbcp.class. getResourceAsStream ("Db.properties");9 //loading the property configuration fileTen prop.load (instream); One //Create a data source object directly based on the prop configuration ADataSource Datasouce =Basicdatasourcefactory.createdatasource (prop); - - //Get Connections theCon =datasouce.getconnection (); -Con.preparestatement ("Delete from admin where id=4"). executeupdate (); -}Catch(Exception e) { - e.printstacktrace (); + Throw NewRuntimeException (e); -}finally { + //Close A if(Con! =NULL) at Try { - con.close (); -}Catch(SQLException e) { - e.printstacktrace (); - Throw NewRuntimeException (e); - } in } - to}
Ii. configuration Files (db.properties)
1 url=jdbc:mysql:///jdbc_demo2 driverclassname=com.mysql.jdbc.Driver 3 username=root4 password=root5 initialsize=36 Maxactive=67 maxidle=3000
DBCP Connection Pool