Caveats: Configuration file naming, cannot change c3p0-config
<?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/day16</property>
<property name= "User" >root</property>
<property name= "Password" >root</property>
<property name= "Acquireincrement" >5</property>
<property name= "Initialpoolsize" >10</property>
<property name= "Minpoolsize" >5</property>
<property name= "Maxpoolsize" >20</property>
</default-config>
</c3p0-config>
C3P0 Data Connection Pool
Packagecom.itheima.utils;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties;ImportJavax.sql.DataSource;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classC3p0util { private static DataSource ds =new Combopooleddatasource (); /*** Used to get connections from the pool *@return */ Public Static synchronizedConnection Getconneciton () {Try { returnds.getconnection (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException (e); } } //Close Resource Public Static voidrelease (ResultSet rs,statement st,connection con) {Try { if(rs!=NULL) {rs.close (); RS=NULL;//The purpose is to have the garbage collected by the collector immediately . } } Catch(SQLException e) {e.printstacktrace (); } Try { if(st!=NULL) {st.close (); St=NULL; } } Catch(SQLException e) {e.printstacktrace (); } Try { if(con!=NULL) {con.close (); } } Catch(SQLException e) {e.printstacktrace (); } }}
Test program
Packagecom.itheima.test;Importjava.sql.Connection;Importjava.sql.Statement;ImportCom.itheima.utils.C3P0Util; Public classC3p0test {/** * @paramargs*/ Public Static voidMain (string[] args) {Connection con=NULL; Statement St=NULL; Try{con=C3p0util.getconneciton (); //.....St =con.createstatement (); St.executeupdate (Insert into account values (5, ' CGX ', 100) "); } Catch(Exception e) {e.printstacktrace (); }finally{c3p0util.release (NULL, St, con); } }}
C3P0 data Connection Pool Popular open source