Two connection pool JDBC

Database Connection Pool
    1. concept: in fact, a container (collection), storage containers database connection.
            When a good system 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 object from the container, after the user access to finished, the connection object will be returned to the container.

    2. Benefits:
        1. conserve resources
        2. The user access efficiency

    3. Implementation:
        1. Standard Interface: DataSource javax.sql package under
            1. Method:
                * Get the connection: the getConnection ()
                * return connection: Connection.close (). If the connection object Connection is to obtain from the connection pool, then call Connection.close () method, you will not close the connection. But the return connection

        2. Generally, we do not realize it, there are database vendors to achieve
            1. C3P0: database connection pool technology
            2. Druid: database connection pool implementation technology, provided by Alibaba


    4. C3P0: database connection pool technology
        * step:
            1. import jar package (two) c3p0-0.9.5.2.jar mchange-Commons-Java-0.2.12.jar,
                * do not forget to import database driver jar package
            2. Define the configuration file:
                * Name: c3p0.properties-config.xml or c3p0
                * path: directly to files in the src directory.

            3. Create a core object database connection pool object ComboPooledDataSource
            4. get connected: getConnection

Code:
             // Create a database connection pool object
            the DataSource ComboPooledDataSource new new DS = ();
            // get a connection objects 2.
            Connection ds.getConnection Conn = ();

public class C3P0demo1 {
     public static void main (String [] args) throws SQLException {
         // . 1 creates a database connection object 
        the DataSource DS = new new ComboPooledDataSource ();
         // 2 acquires connection object 
        Connection Conn = ds.getConnection (); 
        the System.out .println (Conn); 
    } 
}

 

5. Druid: database connection pool implementation technology, provided by Ali Baba
        1. Step:
            1. Import-1.0.9.jar jar package Druid
            2. Custom Profile:
                * is in the form of properties
                * can be called any name can be put in any directory
            3. load the configuration file. The Properties
            4. database connection pool object: to get to the plant by DruidDataSourceFactory
            5. obtaining a connection: the getConnection
        * Code:
             // Load Profile 3.
            The Properties Pro new new = the Properties ();
            the InputStream DruidDemo.class.getClassLoader IS = (). the getResourceAsStream ( "druid.properties");
            pro.load (IS);
            .. 4 // get a connection pool object
            the DataSource DruidDataSourceFactory.createDataSource DS = (Pro);
            .. 5 // Get connected
            Connection conn = ds.getConnection();

 Class definition tool
            1. Define a class JDBCUtils
            2 provides static code block loading configuration files, initialize the connection pool object
            providing method
                1. Obtain connection method: obtaining a connection via a database connection pool
                2. releasing resources
                3. The method of obtaining the connection pool

. 1  public class JDBCUtils {
 2  
. 3      // . 1 . Definition member variable the DataSource
 . 4      Private the DataSource static DS;
 . 5  
. 6      static {
 . 7          the try {
 . 8              // . 1 . Load configuration file
 . 9              the Properties Pro = new new the Properties ();
 10              Pro. Load ( . JDBCUtils.class.getClassLoader () the getResourceAsStream ( "druid.properties"));
 . 11              // 2 . Gets the DataSource
 12 is              DS = DruidDataSourceFactory.createDataSource (Pro);
 13 is         } catch (IOException e) {
14             e.printStackTrace();
15         } catch (Exception e) {
16             e.printStackTrace();
17         }
18     }
19 
20     /**
21      * 获取连接
22      */
23     public static Connection getConnection() throws SQLException {
24         return ds.getConnection();
25     }
26 
27     /**
28      * 释放资源
29      */
30     public static void close(Statement stmt,Connection conn){
31        /* if(stmt != null){
32             try {
33                 stmt.close();
34             } catch (SQLException e) {
35                 e.printStackTrace();
36             }
37         }
38 
39         if(conn != null){
40             try {
41                 conn.close();//归还连接
42             } catch (SQLException e) {
43                 e.printStackTrace();
44             }
45         }*/
46 
47        close(null,stmt,conn);
48     }
49 
50 
51     public static void close(ResultSet rs , Statement stmt, Connection conn){
52 
53 
54         if(rs != null){
55             try {
56                 rs.close();
57             } catch (SQLException e) {
58                 e.printStackTrace();
59             }
60         }
61 
62 
63         if(stmt != null){
64             try {
65                 stmt.close();
66             } catch (SQLException e) {
67                 e.printStackTrace();
68             }
69         }
70 
71         if(conn != null){
72             try {
73                 conn.close();//归还连接
74             } catch (SQLException e) {
75                 e.printStackTrace();
76             }
77         }
78      }
 79  
80      / * *
 81       * Get connection pool
 82       * / 
83  
84      public static getDataSource the DataSource () {
 85          return   DS;
 86      }
 87  
88 }

JDBC the Spring
    * the Spring Framework simple package JDBC. Providing a simplified JDBCTemplate target JDBC development
    * steps:
        1. Import jar package


        2. Create JdbcTemplate object. Dependent on the data source the DataSource
            * = Template JdbcTemplate new new JdbcTemplate (DS);

        3. a method call to complete JdbcTemplate CRUD operations
            * update (): DML statement is executed. Add, delete, change the statement
            * queryForMap (): Query Results The results are set packaged as map set, the column name as a key, the value of the value recorded as a map to put this package set
                * Note: This method sets the length of the query result only. 1
            * the queryForList (): the query result set of the result set list encapsulating
                * Note: each package record set to a Map, then set Map set loaded to a List
            * query (): the query results, the results encapsulated as a JavaBean
                * query parameters: the RowMapper
                    * we generally use BeanPropertyRowMapper implementation class. JavaBean data to complete the automatic packaging
                    * new BeanPropertyRowMapper <type> (Type .class)
            * queryForObject: query results, the results are wrapped object
                * General Query function used in the polymerization

. 1  public class JDBCTemplateDemo1 {
 2  public static void main (String [] args) {
 . 3      // . 1 introduced into the jar package
 4      // 2 JDBCTemplate create objects
 . 5      the JdbcTemplate Template = new new the JdbcTemplate (JDBCUtils.getDataSource ());
 . 6      // call the method
 7      
8      String SQL = " Update  the User  the SET username = ' Guojia '  the WHERE the above mentioned id = ?";
 9      
10      int  COUNT  =  template.update(sql, 1);
11     System.out.println(count);
12 }
13 }

 



Guess you like

Origin www.cnblogs.com/lsymove/p/11279407.html