当前位置:网站首页>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);
}
}
}
边栏推荐
- SwiftUI 组件之如何实现电话号码掩码隐藏部分的文本字段TextField(教程含源码)
- ECS 5 workflow
- Live broadcast platform software development, JS implementation by alphabetical order
- Win11电脑摄像头打开看不见,显示黑屏如何解决?
- N32替换STM32,这些细节别忽略!
- 6-20漏洞利用-proftpd测试
- PyG搭建异质图注意力网络HAN实现DBLP节点预测
- Mongodb database replication table
- [R language - basic drawing]
- Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
猜你喜欢

Pyg builds heterogeneous graph attention network han to realize DBLP node prediction

As for the white box test, you have to be skillful in these skills~

How to solve the problem that the win11 computer camera cannot be seen when it is turned on and the display screen is black?

How does the mqtt server built with emqx forward data and save it to the cloud database?

Xiaobai must see the development route of software testing

Easynlp Chinese text and image generation model takes you to become an artist in seconds

VIM learning manual

Tikz draw Gantt chart in FJSP -trans necessary

Qt: one signal binds multiple slots

How to write a JMeter script common to the test team
随机推荐
Wechat official account custom sharing and updateappmessagesharedata are all successful. Why is it a link that is shared?
Introduction and advanced MySQL (III)
Pytest custom hook function
C and SQL mixed programming, vs need to download what things
How to solve the problem that the win11 computer camera cannot be seen when it is turned on and the display screen is black?
3、 Uni app fixed or direct to a certain page
QT running image
How does the mqtt server built with emqx forward data and save it to the cloud database?
Is the software testing industry really saturated?
UE4.25 Slate源码解读
BM11 链表相加(二)
Can the training software test be employed
BM16 delete duplicate elements in the ordered linked list -ii
Tikz draw Gantt chart in FJSP -trans necessary
Kotlin:sealed Class detailed explanation of sealed class
SRS4.0安装步骤
QT with line encoding output cout
N32 replaces STM32. Don't ignore these details!
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
SQL custom automatic calculation