JDBC_ database connection pool c3p0

. 1  / ** 
2  * @Description: the TODO (described herein sentence action of this class)
 . 3  * @author aikang
 . 4  * @date 2019/8/26 20:12
 . 5   * / 
. 6  / * 
. 7  1. database connection pool :
 8      1. concept: is actually a container (collection), storage containers database connection
 9            when the system well after initialization, the container is created, the container will apply for some of the connection object when the user to access the database, to get a connection from the container the object, the user accesses the connection object after the return of the container
 10      2. benefits:
 11          1. save resources
 12          2. users efficient access
 13      3. Implementation:
 14          1. standard Interface: DataSource javax.sql packages under
 15              1. method:
 16                  Get connected: the getConnection ()
 . 17                 Return Connection: Connection if the connection object is obtained from the connection pool, name calling Connection.close () method, it will not close the connection, but the return connection
 18          2. Generally, we do not realize it, come by the database vendor achieved
 . 19              1.c3p0: database connection pool
 20                  1. step:
 21                      1. import jar package, two
 22                      2. definitions profile:
 23                          name: c3p0.properties or the config.xml the c3p0-
 24                          paths: directly place the file in the src directory to
 25                      3. Create Object core: database connection pool objects ComboPooledDataSource
 26 is                      4. Get connected: the getConnection () * / 
27  public  class JDBCTest02 {
 28      public  static void main (String [] args) {
 29          Connection Connection = null ;
 30      // 1. Import jar package and two drive
 31      // 2. Define profile
 32      // Create Object core: database connection pool objects ComboPooledDataSource 
33 is      CPS = ComboPooledDataSource new new ComboPooledDataSource ( "the c3p0-the config.xml" );
 34 is          the try {
 35              // 4. Get connected: the getConnection () 
36              connection = cps.getConnection ();
 37 [              System.out.println (connection);
 38 is          } the catch (SQLException E) {
 39             e.printStackTrace();
40         }
41         try {
42             connection.close();
43         } catch (SQLException e) {
44             e.printStackTrace();
45         }
46         cps.close();
47     }
48 }

 

Guess you like

Origin www.cnblogs.com/aikang525/p/11416765.html