c3p0 is an open source JDBC Connection pool that implements the data source and Jndi bindings to support the standard extensions of the JDBC3 specification and JDBC2. The open source projects that currently use it are hibernate,spring and so on.
If you do not know what is the connection pool, you can first go to Baidu a bit. I understand, is a big pool, there are a lot of fish, each fish corresponding to a database connection, you get a fish you get a connection, of course, the fish can not eat, you feel finished and put back. This example is a bit perverted haha, can understand on the line.
Good! Here we go.
First, we start from the most basic, is how to use Java code to connect the database with C3P0, and perform query operations.
First we create a new project: It_xiaorenwu
We're going to do c3p0. Import the C3P0 package first:
1.c3p0-0.9.2.1.jar
2.mchange-commons-java-0.2.3.4.jar
3.mysql-connector-java-5.1.7-bin.jar
4.commons-io-2.0.1.jar
5.junit-4.5.jar
Second, we first prepare the database and data
/*sqlyog Ultimate v11.24 (+ bit) mysql-5.5.24:database-test******************************************************* *!40101 set NAMES UTF8 */;/*!40101 set sql_mode= ' */;/*!40014 set @[email protected] @UNIQUE_ CHECKS, unique_checks=0 */;/*!40014 SET @[email protected] @FOREIGN_KEY_CHECKS, foreign_key_checks=0 */;/*!40101 Set @[email protected] @SQL_MODE, sql_mode= ' No_auto_value_on_zero ' */;/*!40111 SET @[email protected] @SQL_ NOTES, sql_notes=0 */; CREATE DATABASE/*!32312 IF not exists*/' test '/*!40100 DEFAULT CHARACTER SET latin1 */; Use ' test ',/*table structure for table ' user ' */drop table IF EXISTS ' user '; CREATE TABLE ' user ' (' id ' int () NOT NULL auto_increment, ' user_name ' varchar () CHARACTER SET latin1 DEFAULT NULL, ' Password ' varchar (+) CHARACTER set latin1 default NULL, ' address ' varchar (+) CHARACTER set latin1 default NULL, ' ph One_number ' varchar ' CHARACTER SET latin1 default null, ' Create_time ' datetime default NULL, ' UpdatE_time ' datetime default NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment=3 DEFAULT charset=utf8;/*data for the tab Le ' user ' */insert into ' user ' (' id ', ' user_name ', ' password ', ' address ', ' phone_number ', ' create_time ', ' update_time ') VALUES (1, ' Test ', ' cy9rzuyh03pk3k6djie09g== ', ' Test ', ' Test ', ' 2014-03-29 00:48:14 ', ' 2014-03-29 00:48:17 '), (2, ' te2 ', ' cy9rzuyh03pk3k6djie09g== ', ' asdfk ', ' 4156434885 ', ' 2015-08-05 11:09:29 ', ' 2015-08-20 11:09:33 ');/*!40101 SET [email protected]_sql_mode */;/*!40014 Set [email protected]_foreign_key_checks */;/*!40014 set [email Protected]_unique_checks */;/*!40111 SET [email protected]_sql_notes */;
Let's start with the first simple Java example: by reading the properties configuration file. Then get the C3P0 connection.
First we prepare a db.properties , modify the user name and password to suit your needs.
Jdbcdriver=com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/testusername=rootpassword=root
To create a new class: C3p0demo1
Package Day12;import Java.beans.propertyvetoexception;import Java.io.ioexception;import java.io.InputStream;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Java.util.properties;import Javax.sql.datasource;import Org.junit.test;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3p0demo1 {@Testpublic void getconnection () throws IOException , Propertyvetoexception, sqlexception{
<span style= "White-space:pre" ></span>//first read the configuration file information, Properties prop = new properties () for configuration c3p0, inputstream in = C3p0Demo1.class.getClassLoader (). getResourceAsStream (" Db.properties ");p rop.load (in);//load information Combopooleddatasource comb = new Combopooleddatasource ();// The following is the starting configuration Comb.setdriverclass (Prop.getproperty ("Jdbcdriver")); Comb.setjdbcurl ("url"); Comb.setuser (Prop.getproperty ("jdbc:mysql://localhost:3306/test")); Comb.setpassword (Prop.getProperty ("root")); Connection con = comb.getconnection ();//Take a fish from C3P0, ah yuck! What fish? Connect a database//execute a query statement string sql = "SELECT * from user"; PreparedStatement PS = con.preparestatement (SQL); ResultSet rs = Ps.executequery ();//Print database information while (Rs.next ()) {System.out.println (rs.getstring (1)); System.out.println (rs.getstring (2)); System.out.println (Rs.getstring (3)); System.out.println (Rs.getstring (5)); System.out.println (rs.getstring (6)); System.out.println (rs.getstring (7)); }//Closing the connection
<span style= "White-space:pre" ></span><pre name= "code" class= "java" style= "Color:rgb (51, 51, 51); font-size:14px; line-height:24px; text-indent:28px; " >
Con.close ();//What this is, this is to connect the connection back to the connection pool instead of closing the connection in.close (); Comb.close (); Rs.close ();p s.close ();} }
Second method: (similar to, but more concise)
we need to create a new configuration file: c3p0.properties ( Note: The name is fixed. C3P0 only recognize this name by default )
C3p0.driverclass=com.mysql.jdbc.driverc3p0.jdbcurl=jdbc:mysql://localhost:3306/testc3p0.user=rootc3p0.password =root
ok! We create a new class:C3p0demo2
Package Day12;import Java.beans.propertyvetoexception;import Java.io.ioexception;import java.io.InputStream;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Java.util.properties;import Javax.sql.datasource;import Org.junit.test;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3p0demo2 {@Testpublic void getconbyc3p0 () throws SQLException {DataSource ds = new Combopooleddatasource ();//c3p0 read the config file himself, we don't do anything connection con = ds.getconnection ();// We take a fish directly System.out.println ("Con:" +con); String sql = "SELECT * from user"; PreparedStatement PS = con.preparestatement (SQL); ResultSet rs = Ps.executequery (); while (Rs.next ()) {System.out.println (rs.getstring (1)); System.out.println (rs.getstring (2)); System.out.println (Rs.getstring (3)); System.out.println (Rs.getstring (5)); System.out.println (rs.getstring (6)); System.out.println (rs.getstring (7)); }//Close Connection con.close ();//What this is, this is to connect the connection back to the connection pool instead of closing the connection rs.close ();p s.close ();}}
Ok! Next we talk about how to configure c3p0! with XML
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Beginner is useful: c3p0 of database connection pool