Java uses c3p0 to manage database connection pools and c3p0 database connections
Database Connection Pool class, used to obtain database connections. The Singleton mode ensures that all connections are managed only through one connection pool.
package com.mousewheel.dbcon;import java.io.InputStream;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DBPool { private static DBPool instance; private ComboPooledDataSource dataSource; static{ instance = new DBPool(); } private DBPool() { try { dataSource = new ComboPooledDataSource(); Properties prop = new Properties(); InputStream in = DBPool.class.getClassLoader().getResourceAsStream("db.properties"); prop.load(in); dataSource.setDriverClass(prop.getProperty("jdbcdriver")); dataSource.setJdbcUrl(prop.getProperty("url")); dataSource.setUser(prop.getProperty("username")); dataSource.setPassword(prop.getProperty("password")); } catch (Exception e) { e.printStackTrace(); } } public static DBPool getInstance(){ return instance; } public Connection getConnection() throws SQLException { return dataSource.getConnection(); }}
DBUtil class, packaging DBPool, simplified use
package com.mousewheel.dbcon;import java.sql.Connection;import java.sql.SQLException;public class DbUtil { public static Connection getConnection() throws SQLException { DBPool pool = DBPool.getInstance(); return pool.getConnection(); }}
Test code, compared with the case where the connection pool is not used
Package com. mousewheel. dbcon; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. util. properties; import java. io. inputStream; import com. mousewheel. dbcon. dbUtil; class App {public static void main (String [] args) {// use c3p0 connection pool try {for (int I = 0; I <100; I ++) {long beginTime = System. currentTimeMillis (); Connection con = DbUtil. getConnection (); // execute the query statement String SQL = "select * from student"; PreparedStatement ps = con. prepareStatement (SQL); ResultSet rs = ps.exe cuteQuery (); while (rs. next () {// System. out. println (rs. getString (1);} con. close (); long endTime = System. currentTimeMillis (); System. out. println (String. format ("% s times, % s", I, (endTime-beginTime) ;}} catch (Exception e) {e. printStackTrace ();} // jdbc without using conection pool try {for (int I = 0; I <100; I ++) {long beginTime = System. currentTimeMillis (); Properties prop = new Properties (); InputStream in = App. class. getClassLoader (). getResourceAsStream ("db. properties "); prop. load (in); Class. forName (prop. getProperty ("jdbcdriver"); Connection con = DriverManager. getConnection (prop. getProperty ("url"), prop. getProperty ("username"), prop. getProperty ("password"); String SQL = "select * from student"; PreparedStatement ps = con. prepareStatement (SQL); ResultSet rs = ps.exe cuteQuery (); while (rs. next () {// System. out. println (rs. getString (1);} con. close (); long endTime = System. currentTimeMillis (); System. out. println (String. format ("% s times, % s", I, (endTime-beginTime) ;}} catch (Exception e) {e. printStackTrace ();}}}