当前位置:网站首页>Encapsulation of JDBC connection and disconnection database
Encapsulation of JDBC connection and disconnection database
2022-06-29 00:05:00 【Texas squirrel】
The code for the implementation class is as follows
public class LinkClose {// Create connection close class
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=" Connect the password ";
static{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection link(){// Connect to database
Connection conn=null;
try {
conn= DriverManager.getConnection(URL,USER,PASS);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void close(PreparedStatement ps, Connection conn){// Override close method , Because some methods do not need to be used ResultSet. At this point, it becomes a method that supports two or three parameters at the same time
close(null,ps,conn);
}
public static void close(ResultSet result, PreparedStatement ps, Connection conn){// Close the database
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();
}
}
}
}
边栏推荐
- FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
- Blue Bridge Cup top ten common heaven level skill - breath of water The type of one recursion
- Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
- 点击劫持:X-Frame-Options未配置
- Analysis of CSRF Cross Site Request Forgery vulnerability
- 【C Primer Plus第二章课后编程题】
- Learning fuzzy from SQL injection to bypass the latest safe dog WAF
- [buuctf.reverse] 131-135
- 随笔记:重新认识 else if
- Stm32f407 ------- IO pin multiplexing mapping
猜你喜欢

Stm32f407 ------ clock system (systeminit clock initialization, systick tick timer)

每日一题: 数组中数字出现的次数

Yyds dry goods count 【 vs code work record III 】 set vs code format

小白创业做电商,选对商城系统很重要!

TypeScript -- 第一节:基础类型

Quartz explanation and use
![Edge extraction based on Halcon learning [2] circles Hdev routine](/img/e4/e3738d71c2ff5a239a12f67d06e2c9.jpg)
Edge extraction based on Halcon learning [2] circles Hdev routine

Auto encoder

The secondary market is full of bad news. How should the market go next? One article will show you the general trend

oracle 去掉html标签
随机推荐
矩 阵 压 缩
MapReduce案例
What are some tips to improve your interview success rate?
Is it safe and reliable to open a securities account in Yixue school?
TypeScript -- 第二节:变量声明
With notes: re understanding else if
这玩意叫跳表?
【LeetCode】21. 合并两个有序链表 - Go 语言题解
Comics | goodbye, postman! One stop collaboration makes apipost more fragrant!
转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)
JDBC连接、断开数据库的封装
Yyds dry goods count 【 vs code work record III 】 set vs code format
LeetCode每日一题:实现strStr()
三個pwn題
入行数字IC验证后会做些什么?
With notes: insert sort --from WCC
炒股开户万一免五是靠谱么,安全么
TypeScript -- 第六节 泛型
stm32F407-------跑马灯、蜂鸣器
【LeetCode】21. Merge two ordered linked lists - go language solution