当前位置:网站首页>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();
}
}
}
}
边栏推荐
猜你喜欢

随笔记:定义setter和getter的三种方式
![[buuctf.reverse] 131-135](/img/c2/b8b06c8191af2c75bf4ad5c82feaea.png)
[buuctf.reverse] 131-135

How many locks are added to an update statement? Take you to understand the underlying principles

Yyds dry goods inventory building knowledge map from scratch with neo4j (I)

随笔记:模拟类数组(array-like)的方法

Sword finger offer 12 Path in matrix

12.物体检测Mask-Rcnn

Don't ask me how to do UI automation test again

三個pwn題

Phoenix installation tutorial
随机推荐
TypeScript -- 第六节 泛型
websocket-js连接如何携带token验证
Online yaml to JSON tool
MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle
Typescript -- Section 1: basic types
每日一题:消失的数字
Mysql-5.7.30-winx64 installation free download and installation tutorial
Stm32f407 ------ running lamp and buzzer
Leetcode 178 Score ranking (June 27, 2022)
Sword finger offer 12 Path in matrix
stm32F407-------串行(串口)通信
Learn binary tree like this
12. Détection d'objets Mask rcnn
Xiaobai's e-commerce business is very important to choose the right mall system!
TypeScript--第四节:函数
这玩意叫跳表?
Picture 64base transcoding and decoding
炒股开户万一免五是靠谱么,安全么
Stm32f407 ------ serial (serial port) communication
Is it safe to open an account for buying stocks online?