三种数据源连接数据库

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包

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值