Three ways to implement a C3P0 connection pool

Source: Internet
Author: User

Jar Package used:

Mysql-connector-java-5.1.30-bin.jar

C3p0-0.9.5.2.jar

Mchange-commons-java-0.2.11.jar


set parameters directly:

C3p0inner.java

Package Com.song.c3p0.db.inner;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;
	
	public class C3p0inner {private static Combopooleddatasource ds; Static initialization blocks are initialized static{try {ds = new Combopooleddatasource ();//create Connection Pool instance Ds.setdriverclass ("Com.mysql.jdbc.Driv ER ");//Set the driver Ds.setjdbcurl (" jdbc:mysql://localhost:3306/cloudhospital ") required to connect to the database,//Set the URL of the connection database Ds.setuser (" Root ");//Set the user name of the connection database Ds.setpassword (" admin ");//Set the password of the connection database Ds.setmaxpoolsize (40);//Set the maximum number of connections for the connection pool Ds.setm Inpoolsize (2);//Set the minimum number of connections for the connection pool ds.setinitialpoolsize (10);//Set the initial connection pool connection number ds.setmaxstatements (100);//Set the cache state of the connection pool
		The maximum number of ment} catch (Exception e) {e.printstacktrace ();
	}}//Gets the connection to the specified database public static Combopooleddatasource getinstance () {return DS; }//Returns a connection from the connection pool public static Connection getconnection () {Connection conn = Null
		try {conn = ds.getconnection ();
		} catch (Exception e) {e.printstacktrace ();
	} return conn;
			}//release resource public static void Realeaseresource (ResultSet rs,preparedstatement ps,connection conn) {if (null! = RS) {
			try {rs.close ();
			} catch (SQLException e) {e.printstacktrace ();
			}} if (null! = PS) {try {ps.close ();
			} catch (SQLException e) {e.printstacktrace ();
		}} try {conn.close ();
		} catch (SQLException e) {e.printstacktrace (); }
	}
}

C3p0innermain.java

Package Com.song.c3p0.db.inner;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException;  public class C3p0innermain {public PreparedStatement setstatement (Connection conn,string sql) {PreparedStatement PS =
		Null
		try {PS = conn.preparestatement (SQL);
		} catch (SQLException e) {e.printstacktrace ();
	} return PS; Public PreparedStatement Setparameter (PreparedStatement ps,object...values) {try {if (null! = values) {for (i
				NT i=1;i<=values.length;i++) {ps.setobject (I, values[i-1]);
		}}} catch (Exception e) {e.printstacktrace ();
	} return PS;
		public static void Main (string[] args) {Connection conn = null;
		PreparedStatement PS = null;
		ResultSet rs = null;
			
			try {conn = c3p0inner.getconnection ();
			
			C3p0innermain C3p0innermain = new C3p0innermain ();
			
			String sql = "SELECT * from Tb_user WHERE ID <?"; PS = C3p0innermain.setstatement (conn, SQL);
			
			PS = C3p0innermain.setparameter (PS, New object[]{20});
			
			rs = Ps.executequery ();
				while (Rs.next ()) {Object OB = Rs.getobject (1);
				Object name = Rs.getobject (4);
			System.out.println ("ID:" + ob + ", Name:" + name ");
		}} catch (Exception e) {e.printstacktrace ();
		}finally{//Release Resources C3p0inner.realeaseresource (RS, PS, conn);
 }
	}
}

To set parameters with a configuration file:

1. Get the parameters from the configuration file

Config.properties

Driverclass = com.mysql.jdbc.Driver
jdbcurl = jdbc:mysql://localhost:3306/cloudhospital
User = root
Password = Admin
maxpoolsize =
minpoolsize = 2
initialpoolsize = 5
maxstatements =
maxidletime = 100

C3p0properties.java

Package com.song.c3p0.db.properties;
Import java.sql.Connection;

Import java.util.Properties;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;
	
	public class C3p0properties {private Combopooleddatasource CPDs;
	
	private static c3p0properties c3p0properties;
	static{c3p0properties = new C3p0properties ();
			
			Public c3p0properties () {try {CPDs = new Combopooleddatasource ();
			Load Config file properties props = new properties ();
			
			Props.load (C3P0Properties.class.getClassLoader (). getResourceAsStream ("config.properties"));
			Cpds.setdriverclass (Props.getproperty ("Driverclass"));
			Cpds.setjdbcurl (Props.getproperty ("Jdbcurl"));
			Cpds.setuser (Props.getproperty ("User"));
			
			Cpds.setpassword (Props.getproperty ("Password"));
			Cpds.setmaxpoolsize (Integer.parseint (Props.getproperty ("maxpoolsize"));
			Cpds.setminpoolsize (Integer.parseint (Props.getproperty ("minpoolsize"));
			Cpds.setinitialpoolsize (Integer.parseint (Props.getproperty ("initialpoolsize")); CPDs. Setmaxstatements (Integer.parseint (Props.getproperty ("maxstatements")));
			
		Cpds.setmaxidletime (Integer.parseint (Props.getproperty ("MaxIdleTime"));
		} catch (Exception e) {e.printstacktrace ();
	}} public static C3p0properties getinstance () {return c3p0properties;
		} public Connection getconnection () {Connection conn = null;
		try {conn = cpds.getconnection ();
		} catch (Exception e) {e.printstacktrace ();
	} return conn;
 }
}

C3popropertiesmain.java

Package com.song.c3p0.db.properties;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException; public class C3popropertiesmain {public PreparedStatement getpreparestatement (Connection conn,string sql) {PREPAREDST
		Atement PS = null;
		try {PS = conn.preparestatement (SQL);
		} catch (SQLException e) {e.printstacktrace ();
	} return PS; Public PreparedStatement setpreparestatementparameter (preparedstatement ps,object ... values) {try {if (null! =
				Values) {for (int i = 1; I <= values.length; i++) {Ps.setobject (I, values[i-1]);
		}}} catch (SQLException e) {e.printstacktrace ();
	} return PS;
			}//release resource public static void Realeaseresource (ResultSet rs,preparedstatement ps,connection conn) {if (null! = RS) {
			try {rs.close ();
			} catch (SQLException e) {e.printstacktrace ();
			}} if (null! = PS) {try {ps.close ();
} catch (SQLException e) {				E.printstacktrace ();
		}} try {conn.close ();
		} catch (SQLException e) {e.printstacktrace ();
		}} public static void Main (string[] args) {Connection conn = null;
		PreparedStatement PS = null;
		ResultSet rs = null;
			
			try {conn = C3p0properties.getinstance (). getconnection ();
			
			String sql = "SELECT * from Tb_user WHERE ID <?";
			
			C3popropertiesmain c3p0instance = new C3popropertiesmain ();
			PS = c3p0instance.getpreparestatement (conn, SQL);
			
			C3p0instance.setpreparestatementparameter (PS, New object[]{20});
			
			rs = Ps.executequery ();
				while (Rs.next ()) {Object obj1 = rs.getobject (1);
				
				Object Obj4 = Rs.getobject (4);
			System.out.println ("ID:" + obj1 + ", NAME:" + obj4);
		}} catch (SQLException e) {e.printstacktrace ();
		}finally{//Release Resources C3popropertiesmain.realeaseresource (RS, PS, conn);
 }
	}
}

2. Do not explicitly get the parameters from the configuration file

C3p0.properties

C3p0.driverclass = com.mysql.jdbc.Driver
c3p0.jdbcurl = jdbc:mysql://localhost:3306/cloudhospital
C3p0.user = root
c3p0.password = admin
c3p0.maxpoolsize =
c3p0.minpoolsize = 3
c3p0.maxstatements =
C3p0.maxidletime = 150

C3p0propertiessimplify.java

Package com.song.c3p0.db.properties;

Import java.sql.Connection;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0propertiessimplify {
	
	private static c3p0propertiessimplify c3p0propertiessimplify;
	
	Private Combopooleddatasource CPDs;
	
	static{
		c3p0propertiessimplify = new C3p0propertiessimplify ();
	}

	Public c3p0propertiessimplify () {
		CPDs = new Combopooleddatasource ();
	}
	
	public static c3p0propertiessimplify getinstance () {
		return c3p0propertiessimplify;
	}
	
	Public Connection getconnection () {
		Connection conn = null;
		try {
			conn = cpds.getconnection ();
		} catch (Exception e) {
			e.printstacktrace ();
		}
		return conn;
	}
}

C3p0propertiessimplifymain.java

Package com.song.c3p0.db.properties;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException; public class C3p0propertiessimplifymain {public PreparedStatement createstatement (Connection conn,string sql) {Prepar
		Edstatement PS = null;
		try {PS = conn.preparestatement (SQL);
		} catch (SQLException e) {e.printstacktrace ();
	} return PS; Public PreparedStatement setparameters (PreparedStatement ps,object...values) {try {if (null! = values) {for (
				int i=1;i<=values.length;i++) {ps.setobject (I, values[i-1]);
		}}} catch (Exception e) {e.printstacktrace ();
	} return PS;
		public static void Main (string[] args) {Connection conn = null;
		PreparedStatement PS = null;
		ResultSet rs = null;
			
			try {conn = C3p0propertiessimplify.getinstance (). getconnection ();
			C3p0propertiessimplifymain c3p0instance = new C3p0propertiessimplifymain (); String sql = "SELECT * from" Tb_user WHERE ID <?
			";
			PS = c3p0instance.createstatement (conn, SQL);
			
			PS = C3p0instance.setparameters (PS, New object[]{20});
			rs = Ps.executequery ();
				while (Rs.next ()) {Object obj1 = rs.getobject (1);
				
				Object Obj4 = Rs.getobject (4);
			System.out.println ("ID:" + obj1 + ", Name:" + obj4);
					}} catch (Exception e) {}finally{try {if (null! = rs) {try {rs.close ();
					} catch (SQLException e) {e.printstacktrace ();
					}} if (null! = PS) {try {ps.close ();
					} catch (SQLException e) {e.printstacktrace ();
				}} try {conn.close ();
				} catch (SQLException e) {e.printstacktrace ();
			}} catch (Exception E2) {e2.printstacktrace ();
 }
		}
	}
}

To set parameters with an XML file:C3p0-config.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<c3p0-config>
	<named-config name= "Myc3p0xml" >
		<property name= "Driverclass" >com.mysql.jdbc.Driver</property>
		<property name= "Jdbcurl" >jdbc:mysql://localhost:3306/cloudhospital</property>
		<property name= "user" >root</ property>
		<property name= "password" >admin</property>
		<property name= "Maxpoolsize" > 20</property>
		<property name= "minpoolsize" >3</property>
		<property name= " Maxstatements ">30</property>
		<property name=" MaxIdleTime ">150</property>
	</ Named-config>
</c3p0-config>

C3p0xmlsimplify.java
Package com.song.c3p0.db.xml;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.sql.SQLException;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;

	public class C3p0xmlsimplify {private static c3p0xmlsimplify c3p0xmlsimplify;
	
	Private Combopooleddatasource CPDs;
	static{c3p0xmlsimplify = new C3p0xmlsimplify ();
	} public C3p0xmlsimplify () {CPDs = new Combopooleddatasource ("Myc3p0xml");
	} public static C3p0xmlsimplify getinstance () {return c3p0xmlsimplify;
		} public Connection getconnection () {Connection conn = null;
		try {conn = cpds.getconnection ();
		} catch (Exception e) {e.printstacktrace ();
	} return conn;
		} public static PreparedStatement setstatement (Connection conn, String sql) {PreparedStatement PS = null;
		try {PS = conn.preparestatement (SQL);
		} catch (SQLException e) {e.printstacktrace ();
	} return PS; } public static PreparedStatement setsqlparameters (PreparEdstatement ps,object...values) {try {if (null! = values) {for (int i = 1; I <= values.length; i++) {p
				S.setobject (i, values[i-1]);
		}}} catch (Exception e) {e.printstacktrace ();
	} return PS;
				} public static void Releasesources (Connection conn, PreparedStatement PS, ResultSet rs) {if (null! = rs) {try {
			Rs.close ();
			} catch (Exception e) {e.printstacktrace ();
			}} if (null! = PS) {try {ps.close ();
			} catch (Exception e) {e.printstacktrace ();
			}} if (null! = conn) {try {conn.close ();
			} catch (Exception e) {e.printstacktrace ();
 }
		}
	}
}

C3p0xmlsimplifymain.java
Package com.song.c3p0.db.xml;

Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

public class C3p0xmlsimplifymain {public

	static void Main (string[] args) {
		Connection conn = null;
		PreparedStatement PS = null;
		ResultSet rs = null;
		try {
			conn = C3p0xmlsimplify.getinstance (). getconnection ();
			
			String sql = "SELECT * from Tb_user WHERE ID <?";
			PS = c3p0xmlsimplify.setstatement (conn, SQL);
			PS = C3p0xmlsimplify.setsqlparameters (PS, New object[]{20});
			
			rs = Ps.executequery ();
			
			while (Rs.next ()) {
				Object obj1 = rs.getobject (1);
				Object Obj4 = Rs.getobject (4);
				
				System.out.println ("ID:" + obj1 + ", Name:" + obj4);
			}
		} catch (Exception e) {
			e.printstacktrace ();
		} finally{
			c3p0xmlsimplify.releasesources (conn, PS, RS);}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.