当前位置:网站首页>JDBC simple encapsulation
JDBC simple encapsulation
2022-07-28 19:19:00 【Enchanted before Yao Jin】
public class JDBC8Util {
private final Connection connection;
private final Statement statement;
private final String url;
private final String username;
private final String password;
static {
// Register the load driver , Manually load bytecode files to JVM in
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 {
// Insert
String insertSql = "insert into dept (name,room,phone) value (' The Marketing Department ','206','11111111');";
int insert = jdbc8Util.insert(insertSql);
System.out.println(" Insert insert\t" + insert);
// modify
String updateSql = "update dept set name=' Finance Department ',room='102',phone='12345566' where id=1";
int update = jdbc8Util.update(updateSql);
System.out.println(" modify update\t" + update);
// Delete
String deleteSql = "delete from dept where id=3";
int delete = jdbc8Util.delete(deleteSql);
System.out.println(" Delete delete\t" + delete);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
jdbc8Util.close();
System.out.println(" Operation is completed , Close database connection ");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
边栏推荐
- Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
- How many of the top ten test tools in 2022 do you master
- About ASM redundancy
- pytest 自定义HOOK函数
- QT - CPP database operation
- Random finite set RFs self-study notes (6): an example of calculation with the formula of prediction step and update step
- VIM learning manual
- What if the content of software testing is too simple?
- Interpretation of ue4.25 slate source code
- Introduction and advanced MySQL (III)
猜你喜欢

Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held

Today in history: Microsoft acquires qdos; Model testing pioneer birth; The first laser typesetting Chinese newspaper

The login interface of modern personal blog system modstartblog v5.4.0 has been revised and the contact information has been added

QT & OpenGL lighting

RTC clock: a calendar

Time waits for no man. The rise of TSDB is at the right time

BM14 链表的奇偶重排

UE4.25 Slate源码解读

Why did wechat change from "small and beautiful" to "big and fat" when it expanded 575 times in 11 years?

6-20漏洞利用-proftpd测试
随机推荐
2022 Niuke multi School Game 2 J. link with arithmetic progress (three points + enumeration)
Special Lecture 6 tree DP learning experience (long-term update)
Creating new projects and adding your own programs
R语言与数据分析实战11-数据的删除
[data analysis] realize SVDD decision boundary visualization based on MATLAB
About ASM redundancy
Pytorch GPU yolov5 reports an error
Haproxy implements proxy configuration
JDBC简单封装
BM14 链表的奇偶重排
使用Xilinx MIG验证硬件DDR设计
一家芯片公司倒在了B轮
UE4.25 Slate源码解读
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
Is it easy to learn the zero foundation of software testing?
What is the future of software testing? How to learn?
Mongodb database shell command execution
2022 Hangdian multi school field 2 1011 DOS card (line segment tree)
From Bayesian filter to Kalman filter (I)
From Bayesian filter to Kalman filter (zero)