C3P0 jar Package and configuration file download address: Click to open the link 1c3p0 introduction
C3P0 is also an open source free connection pool. C3P0 is favored by many people. the use of 2c3p0
The pool class in C3P0 is: Combopooleddatasource.
public void Fun1 () throws Propertyvetoexception, SQLException {
combopooleddatasource ds = new Combopooleddatasource ();
Ds.setjdbcurl ("jdbc:mysql://localhost:3306/mydb1");
Ds.setuser ("root");
Ds.setpassword ("123");
Ds.setdriverclass ("Com.mysql.jdbc.Driver");
Ds.setacquireincrement (5);
Ds.setinitialpoolsize (a);
Ds.setminpoolsize (2);
Ds.setmaxpoolsize (m);
Connection con = ds.getconnection ();
System.out.println (con);
Con.close ();
}
Configuration file Requirements:
L file name: must be called C3p0-config.xml
L File Location: must be under SRC
1.jdbcutils.java
Package cn.itcast.jdbc;
Import java.sql.Connection;
Import java.sql.SQLException;
Import Javax.sql.DataSource;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
public class Jdbcutils {
//configuration file default configuration. Require you to give a c3p0-config.xml ...
private static Combopooleddatasource DataSource = new Combopooleddatasource ();
/**
* Use connection pooling to return a connection object
* @return
* @throws SQLException/public
static Connection getconnection () throws SQLException {return
datasource.getconnection ();
}
/**
* Returns the connection pool object.
* @return
*
/public static DataSource Getdatasource () {return
DataSource
}
}
2.demo3.java
Package cn.itcast.dbutils;
Import java.sql.SQLException;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.handlers.BeanHandler;
Import Org.apache.commons.dbutils.handlers.BeanListHandler;
Import Org.apache.commons.dbutils.handlers.MapHandler;
Import Org.apache.commons.dbutils.handlers.MapListHandler;
Import Org.apache.commons.dbutils.handlers.ScalarHandler;
Import Org.junit.Test;
Import Cn.itcast.jdbc.JdbcUtils; public class Demo3 {@Test public void fun1 () throws SQLException {queryrunner qr = new Queryrunner (jdbcutils.getdata
Source ());
String sql = "INSERT into T_stu values (?,?,?,?)";
Object[] params = {1002, "liSi", "a", "female"};
Qr.update (sql, params); @Test public void Fun2 () throws SQLException {//create Queryrunner, need to provide database connection pool object Queryrunner qr = new Queryrunner (Jd
Bcutils.getdatasource ());
Give sql template String sql = "SELECT * from T_stu where sid=?"; Give the parameter object[] Params = {1001}; resultsethandler<stu> rsh = new resultsethandler<stu> () {////@Override//Public Stu handle (ResultS
Et Rs) throws SQLException {////TODO auto-generated stub//return NULL;//}//}; To execute the query () method, you need to give the result set processor, that is, the Resultsethandler implementation class object//We give the Beanhandler, it implements the Resultsethandler//It needs a type,
It then encapsulates the data in RS into a JavaBean object of the specified type, and then returns JavaBean Stu Stu = qr.query (sql, New beanhandler<stu> (Stu.class), params);
System.out.println (Stu);
/** * Beanlisthandler application, which is a multiline processor * One Stu object per row object. * @throws Exception * * @Test public void Fun3 () throws Exception {queryrunner qr = new Queryrunner (jdbcutils.getdat
Asource ());
String sql = "SELECT * from T_stu";
list<stu> stulist = qr.query (sql, New Beanlisthandler<stu> (Stu.class));
System.out.println (stulist); /** * Maphandler application, which is a single line processor, converts a row into a map object * @throws SQLException * * @Test public void Fun4 () throws Sqlexcep tion {queryrunner qr = new QuEryrunner (Jdbcutils.getdatasource ());
String sql = "SELECT * from T_stu where sid=?";
Object[] params = {1001};
Map map = qr.query (sql, New Maphandler (), params);
SYSTEM.OUT.PRINTLN (map); /** * Maplisthandler, which is a multiline processor, converts each row into a Map, list<map> * @throws SQLException * * @Test public void fun5 ()
Throws SQLException {queryrunner qr = new Queryrunner (Jdbcutils.getdatasource ());
String sql = "SELECT * from T_stu";
list<map<string,object>> maplist = qr.query (sql, New Maplisthandler ());
System.out.println (maplist);
/** * Scalarhandler, which is a single row, is the most appropriate use. * @throws SQLException * * @Test public void Fun6 () throws SQLException {queryrunner qr = new Queryrunner (jdbcutils.
Getdatasource ());
String sql = "SELECT COUNT (*) from T_stu";
* * Integer, Long, BigInteger */Number CNT = (number) qr.query (SQL, New Scalarhandler ());
Long C = Cnt.longvalue ();
System.out.println (c);
}
}
3.c3p0-config.xml (name cannot change)
<?xml version= "1.0" encoding= "UTF-8"?> <c3p0-config> <!--This is the default configuration information--> <default-config> <! --Connecting the four parameters configuration--> <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/mydb3</property> <property
Name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "user" >root</property> <property name= "Password" >123</property> <!--pool parameter configuration--> <property name= "Acquireincrement" >3& lt;/property> <property name= "initialpoolsize" >10</property> <property name= "Minpoolsize" >2 </property> <property name= "maxpoolsize" >10</property> </default-config> <!--specifically for Oracle Configuration information for--> <named-config name= "Oracle-config" > <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/ mydb1</property> <property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name = "User" >root</property> <property name= "Password" >123</property> <property name= "acquireincrement" >3</property> <property name= "Initialpoolsize" >10</property> <property name= "minpoolsize" >2</property> <property name= " Maxpoolsize ">10</property> </named-config> </c3p0-config>