当前位置:网站首页>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();
}
}
}
}
边栏推荐
- Stm32f407 ------ serial (serial port) communication
- [Electronic Experiment 2] simple electronic doorbell
- Stm32f407 ------- RTC real time clock
- 12.物体检测Mask-Rcnn
- 随笔记:定义setter和getter的三种方式
- stm32F407-------跑马灯、蜂鸣器
- mysql 高可用双主同步
- With notes: insert sort --from WCC
- Learning fuzzy from SQL injection to bypass the latest safe dog WAF
- 三个pwn题
猜你喜欢

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

Learning fuzzy from SQL injection to bypass the latest safe dog WAF

好用免费的PPT模板

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

stm32F407-------LCD

The magical zero knowledge proof can not only keep secrets, but also make others believe you!

stm32F407-------IO引脚复用映射

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

stm32F407-------GPIO输入实验

Haskell 配置 VS code 开发环境 (2022年6月)
随机推荐
TypeScript--第五节:类
JVM底层又是如何实现synchronized的
Xiaobai's e-commerce business is very important to choose the right mall system!
The company has a new Post-00 test paper king. The old oilman said that he could not do it. He has been
Easy to use free ppt template
[buuctf.reverse] 131-135
Typescript -- Section 6 generic
Learning fuzzy from SQL injection to bypass the latest safe dog WAF
Give you a project, how will you carry out performance testing (I)
Notes: three ways to define setters and Getters
mysql 高可用双主同步
12. Détection d'objets Mask rcnn
How to solve the database type error in the operation of the servert project?
[buuctf.reverse] 131-135
Stm32f407------- general timer
Chrome浏览器的基本使用
PHP uses curl to download Excel files after logging in to the website
在线买股票开户安全嘛?
【C Primer Plus第二章課後編程題】
[C Primer Plus Chapter II after class programming questions]