「MySQL」自定义数据库连接池和开源数据库连接池的使用

本文介绍了如何自定义数据库连接池,包括配置文件、JDBCUtils工具类以及实现连接池类。通过创建MyDataSource类,初始化10个数据库连接放入池中。在测试中展示了连接池的使用,但在使用后未归还连接,提出了归还连接的问题。接着探讨了通过继承和装饰设计模式改进连接池,最后提出适配器设计模式来简化代码,减少重复实现的方法。
摘要由CSDN通过智能技术生成

配置文件 config.properties

driverClass=com.mysql.jdbc.Driver

url=jdbc:mysql://主机名:3306/数据库名

username=用户名

password=密码

JDBCUtils工具类

public class JDBCUtils {

private JDBCUtils() {} //构造函数私有化

private static String driverClass;

private static String url;

private static String username;

private static String password;

private static Connection con;

static {

try {

InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream(“config.properties”);

Properties properties = new Properties();

properties.load(is);

driverClass = properties.getProperty(“driverClass”);

url = properties.getProperty(“url”);

username = properties.getProperty(“username”);

password = properties.getProperty(“password”);

Class.forName(driverClass);

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

public static Connection getConnection() { //获取连接对象

try {

con = DriverManager.getConnection(url, username, password);

} catch (SQLException e) {

e.printStackTrace();

}

return con;

}

//关闭连接(有查询结果集)

public static void close(Connection con, Statement stat, ResultSet res) {

if (con != null) {

try {

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (stat != null) {

try {

stat.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (res != null) {

try {

re

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

s.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

//关闭连接(无查询结果集)

public static void close(Connection con, Statement stat) {

close(con, stat, null);

}

}

实现连接池类

======

定义一个连接池类并实现 java.sql.DataSource 接口。

Connection getConnection(); //获取数据库连接对象

public class MyDataSource implements DataSource{

//定义集合容器,用于保存多个数据库连接对象

//使用Collections 工具类实现集合的线程同步

private static List pool = Collections.synchronizedList(new ArrayList());

//静态代码块,生成10个数据库连接保存到集合中

static {

for (int i = 0; i < 10; i++) {

Connection con = JDBCUtils.getConnection();

pool.add(con);

}

}

//返回连接池的大小

public int getSize() {

return pool.size();

}

//从池中返回一个数据库连接

@Override

public Connection getConnection() {

if(pool.size() > 0) {

//从池中获取数据库连接

return pool.remove(0);

}else {

throw new RuntimeException(“连接数量已用尽”);

}

}

@Override

public Connection getConnection(String username, String password) throws SQLException {

return null;

}

@Override

public T unwrap(Class iface) throws SQLException {

return null;

}

@Override

public boolean isWrapperFor(Class<?> iface) throws SQLException {

return false;

}

@Override

public PrintWriter getLogWriter() throws SQLException {

return null;

}

@Override

public void setLogWriter(PrintWriter out) throws SQLException {

}

@Override

public void setLoginTimeout(int seconds) throws SQLException {

}

@Override

public int getLoginTimeout() throws SQLException {

return 0;

}

@Override

public Logger getParentLogger() throws SQLFeatureNotSupportedException {

return null;

}

}

自定义连接池的测试

=========

public class MyDataSourceTest {

public static void main(String[] args) throws SQLException {

MyDataSource dataSource = new MyDataSource();

System.out.println(“使用前连接池数量:” + dataSource.getSize());

Connection con = dataSource.getConnection();

String sql = “select * from emp”;

PreparedStatement pst = con.prepareStatement(sql);

ResultSet res = pst.executeQuery();

while (res.next()) {

String ename = res.getString(“ename”);

String job = res.getString(“job”);

String hiredate = res.getString(“hiredate”);

System.out.println(“ename:” + ename + “\t job:” + job + “\t hiredate:” + hiredate);

}

res.close();

pst.close();

con.close();

System.out.println(“使用后连接池数量:” + dataSource.getSize());

}

}

输出:

使用前连接池数量:10

ename:SMITH job:CLERK hiredate:1980-12-17

ename:ALLEN job:SALESMAN hiredate:1981-02-20

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值