当前位置:网站首页>JDBC连接、断开数据库的封装
JDBC连接、断开数据库的封装
2022-06-28 23:59:00 【德克萨斯的松鼠】
实现类的代码如下
public class LinkClose {//创建连接关闭类
private static final String DRIVER="com.mysql.cj.jdbc.Driver";
private static final String URL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
private static final String USER="root";
private static final String PASS="连接密码";
static{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection link(){//连接数据库
Connection conn=null;
try {
conn= DriverManager.getConnection(URL,USER,PASS);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void close(PreparedStatement ps, Connection conn){//重写关闭方法,因为有些方法不需要用到ResultSet。此时重写后就变成了同时支持两个或三个参数的方法
close(null,ps,conn);
}
public static void close(ResultSet result, PreparedStatement ps, Connection conn){//关闭数据库
if(result!=null){
try {
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
边栏推荐
猜你喜欢

好用免费的PPT模板

stm32F407-------跑马灯、蜂鸣器

ctfshow XSS

stm32F407-------LCD

stm32F407-------电容触摸按键

Xiaobai's e-commerce business is very important to choose the right mall system!

What pitfalls should be avoided in the job interview for the operation post in 2022?

Implementation of dynamic timer for quartz

从SQL注入绕过最新安全狗WAF中学习fuzz

TypeScript -- 第一节:基础类型
随机推荐
Stm32f407------- general timer
[C Primer Plus Chapter II after class programming questions]
[Electronic Experiment 2] simple electronic doorbell
《英语语法新思维 基础版2》读书笔记(一)
Yyds dry goods inventory building knowledge map from scratch with neo4j (I)
stm32F407-------寄存器地址名称映射分析
Sword finger offer 12 Path in matrix
MySQL connection query is easy to understand
stm32F407-------GPIO输入实验
Stm32f407----- capacitive touch button
Implementation of dynamic timer for quartz
"Five considerations" for safe use of the Internet
What pitfalls should be avoided in the job interview for the operation post in 2022?
入行数字IC验证后会做些什么?
How to make two objects or arrays equal
三个pwn题
这玩意叫跳表?
在线买股票开户安全嘛?
PHP uses endroid/qrcode QR code to generate, and Gd library generates sharing posters
随笔记:插入排序 --from wcc