c3p0配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 数据源名称代表连接池 -->
<named-config name="DataSource">
<!-- 驱动类 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<!-- url-->
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/examination_system_online</property>
<!-- 用户名 -->
<property name="user">root</property>
<!-- 密码 -->
<property name="password">123456</property>
<!-- 每次增长的连接数-->
<property name="acquireIncrement">5</property>
<!-- 初始的连接数 -->
<property name="initialPoolSize">10</property>
<!-- 最小连接数 -->
<property name="minPoolSize">5</property>
<!-- 最大连接数 -->
<property name="maxPoolSize">50</property>
<!-- 可连接的最多的命令对象数 -->
<property name="maxStatements">5</property>
<!-- 每个连接对象可连接的最多的命令对象数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
c3p0连接数据库工具类
DBCP2配置文件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/examination_system_online
username=root
password=123456
//连接数可改
initialSize=0
maxTotal=20
maxIdle=8
minIdle=0
maxWaitMillis=-1
DBCP2连接数据库工具类
德鲁伊配置文件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/examination_system_online?rewriteBatchedStatements=true
username=root
password=123456
initialSize=0
maxActive=20
maxWait=1000
德鲁伊连接数据库工具类
连接数据库通用方法
import cn.kgc.utils.C3P0Utils;
import cn.kgc.utils.DBCP2Utils;
import cn.kgc.utils.DruidUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
//使用DBCP2数据源连接
public boolean getConnection(){
try{
connection = DBCP2Utils.getConnection();
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
//使用德鲁伊数据源连接
public boolean getConnection2(){
try{
connection = DruidUtils.getConnection();
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
//使用c3p0数据源连接
public boolean getConnection3(){
try{
connection = C3P0Utils.getConnection();
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
public boolean close(){
try{
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
try{
if(pstmt != null){
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
try{
if(connection != null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
//数据库通用增删改方法
public ResultSet executeQuery(String sql,Object[] params){
if(getConnection3()) {
try {
pstmt = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
//数据库通用查询方法
public int update(String sql,Object[] params){
int updateRow = 0;
if(getConnection2()) {
try {
pstmt = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
updateRow = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
return updateRow;
}
}
所需相关jar包