Database connection pool
1. The concept
- Essentially, it holds database connection objects
Connection
The container - When the application starts, the container is created and applies for connection objects
Connection
When the user accesses the database, the connection object is obtained from the container. After user access, the connection object is returned to the container to reuse the connection object and improve efficiency.
2. Implement
- The database vendor implements the interface
DataSource
- C3P0: Database connection pooling technology
- Druid: Database connection pooling technology
3. Get/return the connection
- Get a connection:
dataSource.getConnection()
- Return connection:
connection.close()
4. Use of C3P0
- Import the jar package
C3p0-0.9.5.2. Jar
.McHange - Commons - Java - 0.2.12. Jar
And database driver JAR packageMysql connector - Java - 5.1.37 - bin. The jar
- Define configuration files related to database connection pools (details omitted)
- Properties or c3P0-config.xml
- Path: In the SRC directory
- Create a database connection pool object
DataSource ds = new ComboPooledDataSource();
- Get the connection object
Connection conn = ds.getConnection();
5. Druid
- steps
- Import the jar package
Druid - 1.0.9. Jar
- Defines a configuration file of type properties that can be placed in any directory
- Loading a Configuration File
- Get the database connection pool object from the factory
- Get the connection object
- Import the jar package
- code
try { Properties pro = new Properties(); InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"); if(is ! =null) { pro.load(is); } ds = DruidDataSourceFactory.createDataSource(pro); } catch (Exception e) { e.printStackTrace(); } Copy the code
2: JDBCTemplate
- Import five related JAR packages
- 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 statementqueryForMap()
: Encapsulates the query result set into a map set, with column names as keys and values as values- Note: The result set length of this method query can only be 1
queryForList()
: Encapsulates the query result set as a list- Note:
List<Map<String, Object>>
: Encapsulates each record into a Map collection and loads the Map collection into the List collection
- Note:
query()
: Encapsulates query results as JavaBean objects- Parameter: SQL statement,
New BeanPropertyRowMapper< type >(type.class)
- Using the BeanPropertyRowMapper implementation class, you can complete automatic encapsulation of data into A JavaBean
- Query all records and encapsulate them as a List collection of Emp objects. Example code:
String sql = "select * from emp"; List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class)); for (Emp emp : list) { System.out.println(emp); } Copy the code
- Parameter: SQL statement,
queryForObject()
: Encapsulates the query result as an object- Parameter: SQL statement,
Object type. Class
- Generally used for aggregate function queries
- Parameter: SQL statement,