First, create a file
Create a file named "C3p0-config.xml" in the SRC directory, which reads:
<?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/database name?useunicode=true&characterencoding=utf-8</property>
<property name= " User ">root</property>
<property name=" password "> password </property>
<property name=" Initialpoolsize ">10</property>
<property name=" MaxIdleTime ">30</property>
< Property Name= "Maxpoolsize" >100</property>
<property name= "Minpoolsize" >10</property>
</default-config>
</c3p0-config>
Second, create C3p0utils
/** * @author CSDN_LQR * @TODO C3P0 Tool Class (Database connection Pooling tool) for managing multiple database connection objects.
* Unlike DBCP, C3P0 does not have to manually load the configuration file, just put the configuration file into the SRC directory.
* * public class C3p0utils {//Get a data source (connection pool) private static DataSource ds = new Combopooleddatasource ();
public static DataSource Getdatasource () {return DS; /** * Get Database Connection Object * * @return/public static Connection getconnection () {Connecti
On Conn;
try {conn = ds.getconnection ();
Return conn; The catch (SQLException e) {throw new RuntimeException ("Server busy ...)
"); }/** * Close all resource connections * * @param conn * @param PS * @param rs/public static VO
ID Releaseall (Connection conn, Statement PS, ResultSet rs) {if (conn!= null) {try {
Conn.close ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } conn = null;
} if (ps!= null) {try {ps.close ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
PS = null;
} if (rs!= null) {try {rs.close ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
rs = null;
}
}
}
Third, Attention
The jar packages you need to use C3P0 are: C3p0-0.9.1.2.jar, Mysql-connector-java-5.0.8-bin.jar