First look at c3p0 this connection pool, the biggest advantage can automatically read the default configuration file
<?xml version= "1.0" encoding= "UTF-8"?><c3p0-config> <default-config> < Property Name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Jdbcurl" >JDBC: MySQL://localhost:3306/swift_db </property> <property name= "user" >root </property> <property name= "password" >root</property> <property name= " Initialpoolsize ">20</property> <property name=" acquireincrement ">5</property> <property name= "Maxpoolsize" >50</property> <property name= "Minpoolsize" >5</property > </default-config></c3p0-config>
There are general 4 main options and some other configurations in the configuration file
Just use the implementation class Combopooleddatasource in C3P0 to implement the Javax.sql.DateSource interface to create objects
private static Combopooleddatasource DataSource = new Combopooleddatasource ();
This is the put member variable that, as long as the new NULL parameter will automatically call the XML configuration file, the configuration file needs tags <default-config>
You can also call a named profile, which requires a configuration file <named-config>
<?xml version= "1.0" encoding= "UTF-8"?><c3p0-config> <named-config> <property name= " Driverclass ">com.mysql.jdbc.Driver</property> <property name=" Jdbcurl ">jdbc:mysql:// localhost:3306/swift_db </property> <property name= "user" >root</property > <property name= "password" >root</property> <property name= "Initialpoolsize" >20< /property> <property name= "acquireincrement" >5</property> <property name= "maxpoolsize ">50</property> <property name=" minpoolsize ">5</property> </named-config> </c3p0-config>
C3P0 is generally made into a tool class for easy invocation
Packagecom.swift.base;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJavax.sql.DataSource;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classC3p0utils {//Data source Connection pool C3P0 function do not need to establish a connection every time, wasting resources and waiting time, the connection is done in the connection pool, when needed to obtain//c3p0 connection pooling can automatically read related properties through profile c3p0-config.xml files//C3P0 Connection pooling is an implementation class for Javax.sql.DataSource in Java, where the Getconnection method must be implemented Private StaticCombopooleddatasource DataSource =NewCombopooleddatasource ("Swift"); //ways to get a data source (connection pool) Public StaticDataSource Getdatasource () {returnDataSource; } //A conn resource is obtained from the connection pool. Public StaticConnection getconnection ()throwsException {datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setjdbcurl ("Jdbc:mysql://localhost:3306/swift_db"); Datasource.setuser ("Root"); Datasource.setpassword ("Root"); Datasource.setacquireincrement (5); Datasource.setinitialpoolsize (20); Datasource.setminpoolsize (5); Datasource.setmaxpoolsize (50); Connection Conn=datasource.getconnection (); returnConn; } Public Static voidClose (Connection conn, Statement stmt, ResultSet rs) {if(rs! =NULL) { Try{rs.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(stmt! =NULL) { Try{stmt.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(Conn! =NULL) { Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); } } }}
This has been called the XML configuration file, each time the call connection will come over the property configuration, wasting resources, you can use static code block, only one call
PackageCom.swift.jinji;Importjava.beans.PropertyVetoException;Importjava.sql.Connection;Importjava.sql.SQLException;ImportJavax.sql.DataSource;ImportCom.mchange.v2.c3p0.ComboPooledDataSource;/*use C3P0 to obtain 10 connection object requirements: Do not use a configuration file*/ Public classC3p0_10con {Private StaticCombopooleddatasource DataSource =NewCombopooleddatasource (); Static { Try{Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); } Catch(propertyvetoexception e) {e.printstacktrace (); } datasource.setjdbcurl ("Jdbc:mysql://localhost:3306/swift_db"); Datasource.setuser ("Root"); Datasource.setpassword ("Root"); Datasource.setinitialpoolsize (20); Datasource.setacquireincrement (5); Datasource.setmaxpoolsize (50); Datasource.setminpoolsize (5); } Public Static voidMain (string[] args)throwspropertyvetoexception { for(inti=0;i<10;i++) {System.out.println (c3p0_10con.getconnection ()); } } Public StaticDataSource Getdatasource () {returnDataSource; } Public StaticConnection getconnection () {Connection con=NULL; Try{con=datasource.getconnection (); } Catch(SQLException e) {e.printstacktrace (); } returncon; } Public Static voidClose (Connection con) {if(con!=NULL) { Try{con.close (); } Catch(SQLException e) {e.printstacktrace (); } } }}
With Dbutils Qureyrunner class and Resultsethandler class use, more convenient database additions and deletions and query
PackageCom.swift.jinji;Importjava.sql.SQLException;Importjava.util.Arrays;Importjava.util.List;ImportJava.util.Map;ImportOrg.apache.commons.dbutils.QueryRunner;ImportOrg.apache.commons.dbutils.ResultSetHandler;ImportOrg.apache.commons.dbutils.handlers.ArrayHandler;ImportOrg.apache.commons.dbutils.handlers.ArrayListHandler;ImportOrg.apache.commons.dbutils.handlers.BeanHandler;ImportOrg.apache.commons.dbutils.handlers.ColumnListHandler;ImportOrg.apache.commons.dbutils.handlers.KeyedHandler;ImportOrg.apache.commons.dbutils.handlers.MapHandler;ImportOrg.apache.commons.dbutils.handlers.MapListHandler;ImportOrg.apache.commons.dbutils.handlers.ScalarHandler;Importcom.swift.base.C3p0Utils;ImportCom.swift.domain.Puppy;/*queries the first piece of data in the user table. and encapsulates the data into an array of objects. */ Public classQurey_puppy { Public Static voidMain (string[] args) {queryrunner qr=NewQueryrunner (C3p0utils.getdatasource ()); String SQL= "SELECT * from Puppy;"; Resultsethandler<Puppy> rsh=NewBeanhandler<puppy> (Puppy.class); Resultsethandler<Object[]> rsh1=NewArrayhandler (); Resultsethandler<List<Object[]>> rsh2=NewArraylisthandler (); Resultsethandler<Object> rsh3=NewScalarhandler ("name"); Resultsethandler<map<string, object>> rsh4=NewMaphandler (); Resultsethandler<list<map<string, object>>> rsh5=NewMaplisthandler (); Resultsethandler<List<Object>> rsh6=NewColumnlisthandler ("name"); Resultsethandler<Map<Object,Map<String,Object>>> rsh7=NewKeyedhandler ("id"); Try{Map<object, map<string, object>> result =qr.query (SQL, Rsh7); /*For (object[] puppy:dog) {System.out.println (arrays.tostring (puppy)); }*///System.out.println (result); } Catch(SQLException e) {e.printstacktrace (); } }}
Resultsethandler has a lot of subclasses, which can be put into various containers, after extracting the data from the database, putting the data into an array of objects, arrays of objects, object elements, map collections, map collection lists, object lists, map maps
C3P0 Connection pool (DBCP not readable XML configuration file, obsolete) and two main classes Queryrunner and Resultsethandler in Dbutils in JDBC Foundation step