Introduction to database connection pool c3p0

Source: Internet
Author: User

Introduction to database connection pool c3p0

C3P0 is an open-source JDBC connection pool that implements data source and JNDI binding and supports JDBC3 specifications and standard extensions of JDBC2. Currently, open-source projects using it include Hibernate and Spring.

If you do not know what a connection pool is, you can go to Baidu first. What I understand is that there is a large pool of fish. Each fish corresponds to a database connection. If you get a fish, you will get a connection. Of course, this fish cannot be eaten, you need to put it back after you touch it. This example is a little abnormal. You can understand it.

Good! Now we start.

1. First, we start from the most basic point, that is, how to use java code to connect to the database with c3p0 and perform query operations.

First, create a project: it_xiaorenwu.

To create c3p0, we need to first import the c3p0 package:

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

2. Prepare the database and data first.

 
 
  1. /* 
  2. SQLyog Ultimate v11.24 (32 bit) 
  3. MySQL - 5.5.24 : Database - test 
  4. ********************************************************************* 
  5. */ 
  6.   
  7.   
  8. /*!40101 SET NAMES utf8 */; 
  9.   
  10. /*!40101 SET SQL_MODE=''*/; 
  11.   
  12. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 
  13. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 
  14. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 
  15. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 
  16. CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET latin1 */; 
  17.   
  18. USE `test`; 
  19.   
  20. /*Table structure for table `user` */ 
  21.   
  22. DROP TABLE IF EXISTS `user`; 
  23.   
  24. CREATE TABLE `user` ( 
  25.   `id` int(10) NOT NULL AUTO_INCREMENT, 
  26.   `user_name` varchar(20) CHARACTER SET latin1 DEFAULT NULL, 
  27.   `password` varchar(100) CHARACTER SET latin1 DEFAULT NULL, 
  28.   `address` varchar(100) CHARACTER SET latin1 DEFAULT NULL, 
  29.   `phone_number` varchar(20) CHARACTER SET latin1 DEFAULT NULL, 
  30.   `create_time` datetime DEFAULT NULL, 
  31.   `update_time` datetime DEFAULT NULL, 
  32.   PRIMARY KEY (`id`) 
  33. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 
  34.   
  35. /*Data for the table `user` */ 
  36.   
  37. 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'); 
  38.   
  39. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 
  40. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 
  41. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 
  42. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 

Let's start with the first simple java example: Read the properties configuration file. Then obtain the connection of c3p0.

First, we prepare a db. properties and modify the user name and password as needed.

 
 
  1. jdbcdriver=com.mysql.jdbc.Driver 
  2. url=jdbc:mysql://localhost:3306/test 
  3. username=root 
  4. password=root 

Create a class: C3p0Demo1

 
 
  1. package day12; 
  2.   
  3. import java.beans.PropertyVetoException; 
  4. import java.io.IOException; 
  5. import java.io.InputStream; 
  6. import java.sql.Connection; 
  7. import java.sql.PreparedStatement; 
  8. import java.sql.ResultSet; 
  9. import java.sql.SQLException; 
  10. import java.util.Properties; 
  11.   
  12. import javax.sql.DataSource; 
  13.   
  14. import org.junit.Test; 
  15.   
  16. import com.mchange.v2.c3p0.ComboPooledDataSource; 
  17.   
  18. public class C3p0Demo1 { 
  19.     @Test 
  20.     public void getConnection() throws IOException, PropertyVetoException, SQLException 
  21.     { 

 
 
  1. <Span style = "white-space: pre"> </span> // read the configuration file information for c3p0 configuration.
  2. Properties prop = new Properties ();
  3. InputStream in = C3p0Demo1. class. getClassLoader (). getResourceAsStream ("db. properties ");
  4. Prop. load (in); // load information
  5. ComboPooledDataSource comb = new ComboPooledDataSource (); // The configuration is started below
  6. Comb. setDriverClass (prop. getProperty ("jdbcdriver "));
  7. Comb. setJdbcUrl (prop. getProperty ("url "));
  8. Comb. setUser (prop. getProperty ("jdbc: mysql: /// localhost: 3306/test "));
  9. Comb. setPassword (prop. getProperty ("root "));
  10. Connection con = comb. getConnection (); // get a fish from c3p0, ah! What fish? connect to a database
  11. // Execute the query statement
  12. String SQL = "select * from user ";
  13. PreparedStatement ps = con. prepareStatement (SQL );
  14. ResultSet rs = ps.exe cuteQuery ();
  15. // Print the database information
  16. While (rs. next ())
  17. {
  18. System. out. println (rs. getString (1 ));
  19. System. out. println (rs. getString (2 ));
  20. System. out. println (rs. getString (3 ));
  21. System. out. println (rs. getString (5 ));
  22. System. out. println (rs. getString (6 ));
  23. System. out. println (rs. getString (7 ));
  24. }
  25. // Close the connection

Con. close (); // What is this, this is to return the connection to the connection pool, rather than closing the connection in. close (); comb. close (); rs. close (); ps. close ();}}

Method 2: (similar, but more concise)

We need to create a new configuration file: c3p0. properties (Note: The name is fixed. C3p0 only recognizes this name by default)

 
 
  1. c3p0.driverClass=com.mysql.jdbc.Driver 
  2. c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test 
  3. c3p0.user=root 
  4. c3p0.password=root 

OK! Create a new class: C3p0Demo2

 
 
  1. Package day12;
  2. Import java. beans. PropertyVetoException;
  3. Import java. io. IOException;
  4. Import java. io. InputStream;
  5. Import java. SQL. Connection;
  6. Import java. SQL. PreparedStatement;
  7. Import java. SQL. ResultSet;
  8. Import java. SQL. SQLException;
  9. Import java. util. Properties;
  10. Import javax. SQL. DataSource;
  11. Import org. junit. Test;
  12. Import com. mchange. v2.c3p0. ComboPooledDataSource;
  13. Public class C3p0Demo2 {
  14. @ Test
  15. Public void getConByC3P0 () throws SQLException
  16. {
  17. DataSource ds = new ComboPooledDataSource (); // c3p0 reads the configuration file by ourselves, so we can do nothing.
  18. Connection con = ds. getConnection (); // we can take a fish directly.
  19. System. out. println ("con:" + con );
  20. String SQL = "select * from user ";
  21. PreparedStatement ps = con. prepareStatement (SQL );
  22. ResultSet rs = ps.exe cuteQuery ();
  23. While (rs. next ())
  24. {
  25. System. out. println (rs. getString (1 ));
  26. System. out. println (rs. getString (2 ));
  27. System. out. println (rs. getString (3 ));
  28. System. out. println (rs. getString (5 ));
  29. System. out. println (rs. getString (6 ));
  30. System. out. println (rs. getString (7 ));
  31. }
  32. // Close the connection
  33. Con. close (); // What is this? This is to return the connection to the connection pool, not to close the connection.
  34. Rs. close ();
  35. Ps. close ();
  36. }
  37. }

Related Article

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.