Data connection pool - DBCP

Keywords: JDBC SQL Java MySQL

I think DBCP is a little more complex than c3p0, but DBCP also has a certain share of use, so I recommend learning it

To establish a DBCP connection pool:

1. Import jar package (DBCP needs more jars, four)

Don't mention the jar package for operating the database, as mentioned in the last blog

commons-dbcp2-2.5.0.jar and commons-pool2-2.6.0.jar and commons-logging-1.2.jar

2. Configure the DBCP File. The File is. properties. This does not necessarily find the suffix File. Just create the File

########DBCP configuration file##########
#This configuration file must be encoded in utf-8, or comments will be garbled
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/usta
username=root
password=root
#Number of initial connections
initialSize=30
#Maximum active number
maxTotal=30
maxIdle=10
minIdle=5
#Maximum wait time (MS)
maxWaitMillis=1000
#Whether the connection in the program is recycled by the connection pool after it is not used
#removeAbandoned=true
removeAbandonedOnMaintenance=true
removeAbandonedOnBorrow=true
#The connection will be deleted (in seconds) if it is not used within the specified number of seconds (configured as 1 second for matching the test program)
removeAbandonedTimeout=1

3. Write a DBCP tool class

package com.xx.train.bdqn.jdbc.utils;
//Create DBCP tool class
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

public class DBCPUtils {
	private static DataSource dataSource;
	static {
		try {
			// 1. Load and find the properties file
			InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties");
			// 2. Load input stream
			Properties props = new Properties();
			props.load(is);
			// 3. Create data source
			dataSource = BasicDataSourceFactory.createDataSource(props);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static DataSource getDataSource() {
		return dataSource;
	}

	public static Connection getConnection() throws Exception {
		return dataSource.getConnection();
	}

}

4. Write a test class and insert a piece of data

package com.xx.train.bdqn.jdbc.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class AddDBCP {
	public void updateuser() throws Exception {
		Connection con = null;
		PreparedStatement pstmt =null;
		
		con = DBCPUtils.getConnection();
		String sql = "insert into user value(null,?,?)";
		pstmt = con.prepareStatement(sql);
		pstmt.setString(1, "Zhang San");
		pstmt.setString(2, "123");
		int n = pstmt.executeUpdate();
		if(n>0) {
			System.out.println("Update success");
		}else {
			System.out.println("Update failed");
		}
	}
}

5. Call test class

package com.xx.train.bdqn.jdbc.test;

import com.xx.train.bdqn.jdbc.utils.AddDBCP;

public class TestDBCP {
	public static void main(String[] args) throws Exception {
		AddDBCP u = new AddDBCP();
		u.updateuser();
	}
}
	

 

Posted by DonelleJenae on Sun, 12 Jan 2020 07:35:01 -0800