当前位置:网站首页>JDBC简单封装
JDBC简单封装
2022-07-28 17:19:00 【心醉瑶瑾前】
public class JDBC8Util {
private final Connection connection;
private final Statement statement;
private final String url;
private final String username;
private final String password;
static {
// 注册加载驱动,手动加载字节码文件到JVM中
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public JDBC8Util(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
try {
this.connection = DriverManager.getConnection(url, username, password);
this.statement = connection.createStatement();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static JDBC8Util createJDBC8(String url, String username, String password) {
return new JDBC8Util(url, username, password);
}
public int update(String sql) throws SQLException {
return statement.executeUpdate(sql);
}
public int insert(String sql) throws SQLException {
return statement.executeUpdate(sql);
}
public int delete(String sql) throws SQLException {
return statement.executeUpdate(sql);
}
public ResultSet query(String sql) throws SQLException {
return statement.executeQuery(sql);
}
public void close() throws SQLException {
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
public Connection getConnection() {
return connection;
}
public Statement getStatement() {
return statement;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
public class JDBCTest {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/work0724?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false";
String username = "root";
String password = "admin";
JDBC8Util jdbc8Util = JDBC8Util.createJDBC8(url, username, password);
try {
// 插入
String insertSql = "insert into dept (name,room,phone) value ('市场部','206','11111111');";
int insert = jdbc8Util.insert(insertSql);
System.out.println("插入 insert\t" + insert);
// 修改
String updateSql = "update dept set name='财务部',room='102',phone='12345566' where id=1";
int update = jdbc8Util.update(updateSql);
System.out.println("修改 update\t" + update);
// 删除
String deleteSql = "delete from dept where id=3";
int delete = jdbc8Util.delete(deleteSql);
System.out.println("删除 delete\t" + delete);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
jdbc8Util.close();
System.out.println("操作完成,关闭数据库连接");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
边栏推荐
- Easynlp Chinese text and image generation model takes you to become an artist in seconds
- BM14 链表的奇偶重排
- Software testing dry goods
- The login interface of modern personal blog system modstartblog v5.4.0 has been revised and the contact information has been added
- How to solve the problem that easycvr device cannot be online again after offline?
- Can I get employed after two months of software testing training?
- Remember a uniapp experience
- SwiftUI 组件之如何实现电话号码掩码隐藏部分的文本字段TextField(教程含源码)
- Getting started with QT & OpenGL
- Understanding of PID
猜你喜欢

QT & OpenGL lighting

Efficiency comparison of JS array splicing push() concat() methods
![[radar] radar signal online sorting based on kernel clustering with matlab code](/img/56/1f8e8690b47fc4a1f101d4e530b87f.png)
[radar] radar signal online sorting based on kernel clustering with matlab code
![[R language - basic drawing]](/img/1e/aebf1cbe02c4574671bac6dc2c9171.png)
[R language - basic drawing]

When unity customizes the editor, let the subclass inherit the inspector display effect of the parent class

vim学习手册

6-20漏洞利用-proftpd测试

BM11 链表相加(二)

From Bayesian filter to Kalman filter (I)
![[data analysis] realize SVDD decision boundary visualization based on MATLAB](/img/3b/72bb459a357f836b52aa79171d75fc.png)
[data analysis] realize SVDD decision boundary visualization based on MATLAB
随机推荐
Special Lecture 6 tree DP learning experience (long-term update)
Efficiency comparison of JS array splicing push() concat() methods
2022 Niuke multi School Game 2 J. link with arithmetic progress (three points + enumeration)
The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held
Lookup - lookup of sequential table and ordered table
cv5200无线WiFi通信模块,视频图像传输无线化,实时无线通信技术
QT - CPP database operation
How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
Self cultivation of Electronic Engineers - when a project is developed
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
Cv5200 wireless WiFi communication module, wireless video image transmission, real-time wireless communication technology
【物理应用】大气吸收损耗附matlab代码
Tikz draw Gantt chart in FJSP -trans necessary
New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
2022杭电多校第二场1011 DOS Card(线段树)
BM16 delete duplicate elements in the ordered linked list -ii
From Bayesian filter to Kalman filter (I)
SQL custom automatic calculation
From Bayesian filter to Kalman filter (2)