maven依赖:
<!--DBCP连接池依赖-->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.3</version>
<scope>compile</scope>
</dependency>
配置文件:
后缀为: .properties
#驱动路径
driver=com.mysql.cj.jdbc.Driver
#JDBC连接URL
url=jdbc:mysql://localhost:3306/sqldemo?useSSL=false&serverTimezone=UTC
#账号
username=root
#密码
password=123456
#初始连接池大小
initPoolSize=10
#最大空闲时间
maxIdleTime=20
#最大连接池数
maxPoolSize=100
#最大获取连接时间
maxCreateTime=10;
代码演示:
package com.JDBC;
import org.apache.commons.dbcp.BasicDataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
/**
* @Created with IntelliJ IDEA
* @Description:
* @Package: com.JDBC
* @author: FLy-Fly-Zhang
* @Date: 2019/7/6
* @Time: 20:26
*/
public class DBCPTest {
public static void main(String[] args) {
BasicDataSource dataSource=new BasicDataSource();
//jdk提供的专门读取properties配置文件的类。
Properties properties=new Properties();
try {
//通过反射得到类加载器,将需要的配置文件加载进来。只要是classpath路径下面的均可以被加载
properties.load(DBCPTest.class.getClassLoader().getResourceAsStream("dbcp.properties"));
//读取配置文件中数据库驱动路径
dataSource.setDriverClassName(properties.getProperty("dirver"));
//读取配置文件中url
dataSource.setUrl(properties.getProperty("url"));
//读取配置文件中数据库用户名
dataSource.setUsername(properties.getProperty("username"));
//读取配置文件中用户名对应密码
dataSource.setPassword(properties.getProperty("password"));
//读取配置文件中初始化连接数量
dataSource.setInitialSize(Integer.parseInt(properties.getProperty("initPoolSize")));
//配置连接等待最大超时时间
dataSource.setMaxWait(Long.parseLong(properties.getProperty("maxIdleTime")));
//配置最大并发连接数
dataSource.setMaxActive(Integer.parseInt(properties.getProperty("maxPoolSize")));
//从连接池中取一个连接
//下来就是正常的JDBC操作。
long start=System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
new RunPool(dataSource.getConnection()).run();
}
System.out.println(System.currentTimeMillis()-start);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.JDBC;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Created with IntelliJ IDEA
* @Description:
* @Package: com.JDBC
* @author: FLy-Fly-Zhang
* @Date: 2019/7/7
* @Time: 15:43
*/
public class RunPool {
private Connection connection;
public RunPool(Connection connection){
this.connection=connection;
}
public void run() {
try {
//下来就是正常的JDBC操作。
String sql="select * from Student,SC where Student.SID=SC.SID and Student.Ssex=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,"男");
//提交Sql,并获取返回结果
ResultSet resultSet=statement.executeQuery();
while(resultSet.next()){
String name=resultSet.getString("Sname");
int age=resultSet.getInt("Sage");
int score=resultSet.getInt("score");
//System.out.println("Sname: "+name+" Sage: "+age+" score: "+score);
}
//关闭连接
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}