This article focuses on the following aspects:
- Introduction to database connection pools
- Database connection pool tool implementation
- JDBCTemplate is introduced
- 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 objectsConnection
Is 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
C3P0
: Database connection pooling technology.Druid
: Database connection pool implementation technology, provided by Alibaba.
2.2 Implementation of common data vendors
2.2.1 C3P0
implementation
Steps:
- C3p0-0.9.5.2. jar McHange-commons-java-0.2.12.jar,
- Don’t forget to import the database driver JAR package
- Defining a configuration file:
- Properties or c3P0-config.xml
- Path: Just place the file in the SRC directory.
- Create the core object database connection pool object ComboPooledDataSource
- 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 Druid
implementation
Steps:
- Jar package druid-1.0.9
- Defining a configuration file:
- Is in the form of properties
- You can call it anything, you can put it in any directory
- Load the configuration file. Properties
- Get the Database connection pool object: Get the DruidDataSourceFactory from the factory
- 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:
- Import the jar package
- Create the JdbcTemplate object. Depends on the DataSource
- JdbcTemplate template = new JdbcTemplate(ds);
- 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