With the C3P0 connection pool, you do not need to parse the configuration file yourself, just create a profile with the specified name under the SRC folder.
To create a project:
C3p0.xml configuration file:
<?xml version= "1.0" encoding= "UTF-8"?><c3p0-config><!--default configuration, use this configuration if not specified--><default-config ><!--Basic Configuration--><property name= "Driverclass" >com.mysql.jdbc.driver</property><property name= "Jdbcurl" >jdbc:mysql://127.0.0.1:3306/mydb</property><property name= "user" >root</property> <property name= "Password" >root</property><!--extended configuration--><property name= "Checkouttimeout" > 30000</property><property name= "Idleconnectiontestperiod" >30</property><property name= " Initialpoolsize ">10</property><property name=" MaxIdleTime ">30</property><property name=" Maxpoolsize ">100</property><property name=" minpoolsize ">10</property><property name=" Maxstatements ">200</property></default-config> <!--named configuration--><named-config name=" localhost "><property name=" Driverclass ">com.mysql.jdbc.driver</property><property name=" JdbcUrl ">Jdbc:mysql://127.0.0.1:3306/mydb</property><property name= "User" >root</property><property Name= "Password" >root</property><!--if the data connection in the pool is not enough, how many--><property name= "Acquireincrement" at a time > 5</property><property name= "initialpoolsize" >20</property><property name= "MinPoolSize" > 10</property><property name= "maxpoolsize" >40</property><property name= "maxStatements" >20 </property><property name= "Maxstatementsperconnection" >5</property></named-config><! --Named Configuration 2--><named-config name= "Itcast" ><property name= "Driverclass" >com.mysql.jdbc.driver</ Property><property name= "Jdbcurl" >jdbc:mysql://127.0.0.1:3306/xxx</property><property name= " User ">root</property><property name=" password ">root</property><!--if the pool does not have enough data to connect at a time-- ><property name= "acquireincrement" >5</property><property name= "InitialPoolSize" >20</pRoperty><property name= "minpoolsize" >10</property><property name= "MaxPoolSize" >40</ Property><property name= "maxstatements" >20</property><property name= " Maxstatementsperconnection ">5</property></named-config><!--can have many named configurations X--></c3p0-config >
C3p0test.java:
Package Com.gordon.c3p0;import Java.util.list;import Org.apache.commons.dbutils.queryrunner;import Org.apache.commons.dbutils.handlers.arraylisthandler;import Com.mchange.v2.c3p0.combopooleddatasource;public Class C3p0test {public static void main (string[] args) {try {selectfunction ();} catch (Exception e) {System.out.println (E. GetMessage ());}} public static void Selectfunction () throws Exception {//can use multiple command configurations, if not found, use the default configuration Combopooleddatasource CPDs = new Combop Ooleddatasource ("localhost");//use dbutilsqueryrunner QR = new Queryrunner (CPDs);//structure query languagestring sql = " SELECT * from user ";//Set parameter object[] params = {};//execution sqllist<object[]> objlist = qr.query (sql, New Arraylisthandler ( ), params);//operation result for (object[] obj:objlist) {System.err.println (obj[0] + ":" + obj[1]);}}
JAVA-C3P0 Connection Pool