当前位置:网站首页>JDBC从连接池获取连接(Druid连接池)
JDBC从连接池获取连接(Druid连接池)
2022-07-26 12:20:00 【方十七】
package com.gotion.dataextract.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.gotion.common.core.constant.Constants;
import com.gotion.common.core.constant.ServiceNameConstants;
import com.gotion.common.core.exception.BaseException;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;
/**
* 数据库链接工具类
*
* @author fangzp
* @date 2022-06-16
*/
public class JdbcUtil {
/**
* 从druid数据库连接池获取数据库连接
*
* @param driver 数据库驱动
* @param url 数据库连接串
* @param username 用户名
* @param password 密码
* @return 连接
*/
public static Connection druidDataSource(String driver, String url, String username, String password) {
try {
Properties properties = new Properties();
properties.setProperty("driverClassName", driver);
properties.setProperty("url", url);
properties.setProperty("username", username);
properties.setProperty("password", password);
properties.setProperty("initialSize", "10");
properties.setProperty("maxActive", "20");
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
return dataSource.getConnection();
} catch (Exception e) {
throw new BaseException(ServiceNameConstants.DATA_SERVICE, Constants.ERROR, null, "获取数据库连接错误");
}
}
/**
* 释放连接资源
*
* @param conn 连接
* @param ps 预处理对象
* @param rs 结果集
*/
public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}
边栏推荐
- File类的学习过程中出现的问题及解决方法
- 3D point cloud course (VIII) -- feature point matching
- Use the jsonobject object in fastjason to simplify post request parameter passing
- .NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
- Redis master-slave replication principle
- 结合环境光、接近传感以及红外测距的光距感芯片4530A
- 数据库组成表
- 如何以文本形式查看加密过的信息
- 一些常用的文章写作使用方法和技巧
- 【2243】module_ param.m
猜你喜欢

Pytest interface automation test framework | pytest configuration file

Redis为什么这么快?Redis的线程模型与Redis多线程

Microsoft has shut down two attack methods: Office macro and RDP brute force cracking

MySQL组合索引(多列索引)使用与优化

JSJ-3/AC220V时间继电器

扫雷小游戏——轻松玩上瘾(C语言版)

HTAP comes at a price

2、 Container_

论文阅读-MLPD:Multi-Label Pedestrian Detector in Multispectral Domain(海康威视研究院实习项目)

FPGA入门学习(二) - 二选一的选择器
随机推荐
羽毛球馆的两个基础设施你了解多少?
Pytest interface automated testing framework | confitest.py
Data query of MySQL (aggregate function)
Tencent cloud and smart industry business group (CSIG) adjusted the organizational structure and established the digital twin product department
使用fastJson中的JSONObject对象简化POST请求传参
数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
Ssj-21b time relay
Oracle AWR 报告脚本:SQL ordered by Elapsed Time
Problems encountered in byte stream exercises and Solutions
Use of strjoin function in MATLAB
若有声明”int x=5,y=1;”,则表达式x<y?x++:y++的结果是:
Pytest interface automated testing framework | introduction to fixture of pytest
海外APP推送(下篇):海外厂商通道集成指南
The significance of Internet of things device encryption
空洞卷积详解(输入输出大小分析)
Flutter 学习之路
Customize browser default right-click menu bar
FPGA入门学习(三)- 38译码器
Beauty salon management system unified management system?
JVM内存溢出和内存泄漏的区别