JDBC之使用DBCP数据源获取数据库连接

时间:2022-09-12 11:49:25

一、导入如下三个jar包,并添加到类路径

commons-dbcp-1.2.1.jar, commons-pool-1.2.jar, commons-collections-3.1.jar,注意除了这三个jar包,还需要数据库的驱动jar包。

二、在类路径下编写dbcpconfig.properties文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=root


initialSize=10


maxActive=50


maxIdle=20


minIdle=5


maxWait=60000




connectionProperties=useUnicode=true;characterEncoding=gbk


defaultAutoCommit=true


defaultReadOnly=


defaultTransactionIsolation=READ_UNCOMMITTED
三、编写JdbcUtils类

package cn.itcast.jdbc;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

/**
* Jdbc工具类
*/
public final class JdbcUtils {

private static DataSource dataSource = null;

/**
* 构造器私用,防止直接创建对象,
* 当然通过反射可以创建
*/
private JdbcUtils(){

}

//保证只是注册一次驱动
static{
try {
Class.forName("com.mysql.jdbc.Driver");
//获取DBCP数据源
Properties props = new Properties();
InputStream is = JdbcUtils.class.getClassLoader()
.getResourceAsStream("dbcpconfig.properties");
props.load(is);
//使用工厂方法创建数据库,这样即使要改,也只用修改某一处
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}

/**
* 获取连接
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

/**
* 释放资源
*/
public static void free(ResultSet rs, Statement st, Connection conn) {
//规范的关闭连接的方式
try{
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try{
if(st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
四、编写测试类进行测试

package cn.itcast.jdbc.datasource;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import cn.itcast.jdbc.JdbcUtils;

/**
* 用于测试数据类
*/
public class TestDataSource {

/**
* 测试在使用数据库连接池的情况下创建数据库连接
* @throws SQLException
*/
@Test
public void testCreateConn() throws SQLException {
for(int i = 0; i < 10; i ++) {
Connection conn = JdbcUtils.getConnection();
System.out.println(conn);
System.out.println(conn.getClass().getName());
System.out.println(conn.getClass().getSimpleName());
JdbcUtils.free(null, null, conn);
}
}

}
控制台打印结果:

jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper
jdbc:mysql://localhost:3306/jdbc, UserName=root@localhost, MySQL-AB JDBC Driver
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
PoolGuardConnectionWrapper