This article focuses on the following aspects:

  1. Introduction to database connection pools
  2. Database connection pool tool implementation
  3. JDBCTemplate is introduced
  4. The use of JDBCTemplate

1. Database connection pool

1.1 concept

A database connection pool is actually a container (collection) that holds database connections. When the system is initialized, the container is created, the container will apply for some connection objects, when the user to access the database, from the container to obtain the connection object, after the user access, the connection object will be returned to the container.

1.2 advantage

1. Save resources. 2Copy the code

2. Database connection pool tool implementation

2.1 implementation

Standard interface: DataSource (javax.sql)

Methods:

  • Get a connection:getConnection()
  • Return connection:Connection.close(). If you connect objectsConnectionIs obtained from the connection pool and is calledConnection.close()Method, the connection is no longer closed. It is returned to the connection pool.

We generally do not implement, mainly use

  1. C3P0: Database connection pooling technology.
  2. Druid: Database connection pool implementation technology, provided by Alibaba.

2.2 Implementation of common data vendors

2.2.1 C3P0implementation

Steps:

  1. C3p0-0.9.5.2. jar McHange-commons-java-0.2.12.jar,
    • Don’t forget to import the database driver JAR package
  2. Defining a configuration file:
    • Properties or c3P0-config.xml
    • Path: Just place the file in the SRC directory.
  3. Create the core object database connection pool object ComboPooledDataSource
  4. Get the connection: getConnection
    • 1. Create a database connection pool object
    • DataSource ds = new ComboPooledDataSource();
    • //2. Obtain the connection object
    • Connection conn = ds.getConnection();

2.2.2 Druidimplementation

Steps:

  1. Jar package druid-1.0.9
  2. Defining a configuration file:
    • Is in the form of properties
    • You can call it anything, you can put it in any directory
  3. Load the configuration file. Properties
  4. Get the Database connection pool object: Get the DruidDataSourceFactory from the factory
  5. Get the connection: getConnection

2.3 Code test

  • Get multiple database connection objects

  • Gets more than the maximum number of database connection objects

  • When the previous database connection object is released, it can be used again

3. The JDBCTemplate is introduced

The Spring framework’s simple encapsulation of JDBC. Provides a JDBCTemplate object to simplify JDBC development. Steps:

  1. Import the jar package
  2. Create the JdbcTemplate object. Depends on the DataSource
    • JdbcTemplate template = new JdbcTemplate(ds);
  3. Call the JdbcTemplate method to complete CRUD operations
    • Update (): Executes the DML statement. Add, delete, and change statements
    • QueryForMap (): The query result encapsulates the result set as a map, the column name as a key, and the value as a value to encapsulate the record as a map
      • Note: The result set length of this method query can only be 1
    • QueryForList (): Query result encapsulates the result set as a list collection
      • Note: Encapsulate each record as a Map collection and load the Map collection into the List collection
    • Query (): Encapsulates the query result as a JavaBean object
      • Parameter to query: RowMapper
        • Typically we implement the class using BeanPropertyRowMapper. It can complete automatic encapsulation of data to JavaBean
        • New BeanPropertyRowMapper< type >(type.class)
    • QueryForObject: A query result that encapsulates as an object
      • Generally used for aggregate function queries

4. Use of JDBCTemplate

  • Use the JDBCTemplate to manipulate the database
  • Insert data

Note: The source code for this article is hosted on Github