当前位置:网站首页>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();
}
}
}
}
边栏推荐
- 转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)
- Windows平台下安装MySQL(附:Navicat Premium 12 “使用” 教程)
- 【C Primer Plus第二章課後編程題】
- Reading notes of English grammar new thinking Basic Edition 2 (I)
- stm32F407-------通用定时器
- With notes: insert sort --from WCC
- Don't ask me how to do UI automation test again
- LeetCode每日一题:实现strStr()
- [Electronic Experiment 2] simple electronic doorbell
- SQL note 2 [MySQL]
猜你喜欢

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

MapReduce案例

Haskell 配置 VS code 开发环境 (2022年6月)

10、YOLO系列

Huawei's level 22 experts have worked hard for ten years to complete the advanced practical document of cloud native service grid. 6

stm32F407-------通用定时器

stm32F407-------电容触摸按键
![[machine learning] numerical analysis 02 -- finding roots of arbitrary equations](/img/fd/ec82a50017e692ac90f6e8739b28d3.jpg)
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations

Blue Bridge Cup top ten common heaven level skill - breath of water The type of one recursion

stm32F407-------GPIO输入实验
随机推荐
三個pwn題
oracle 去掉html标签
JVM工作原理介绍
TypeScript --第三节:接口
Mysql的四种引擎介绍
每日一练:删除有序数组中的重复项
Summary of software testing cognition
mysql 高可用双主同步
MapReduce case
With notes: re understanding else if
点击劫持:X-Frame-Options未配置
The magical zero knowledge proof can not only keep secrets, but also make others believe you!
What are the virtual machine software? What are their respective roles?
Zoom with mouse wheel
每日一题:移除元素
pymysql. Error get error code and specific error information
ES6:let、const、箭头函数
矩 阵 压 缩
Stm32f407 ------- IO pin multiplexing mapping
12. object detection mask RCNN