当前位置:网站首页>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);
}
}
}
边栏推荐
- Gmoea code operation 2 -- establishment and operation of operation environment
- 关于ASM冗余问题
- Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
- uwb模块实现人员精确定位,超宽带脉冲技术方案,实时厘米级定位应用
- 4、 Interface requests data to update input information interactively
- Solve the critical path in FJSP - with Matlab source code
- The login interface of modern personal blog system modstartblog v5.4.0 has been revised and the contact information has been added
- Creating new projects and adding your own programs
- N32 replaces STM32. Don't ignore these details!
- Live broadcast platform software development, JS implementation by alphabetical order
猜你喜欢

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

PyG搭建异质图注意力网络HAN实现DBLP节点预测

关于ASM冗余问题

Applet applet jump to official account page

【数据分析】基于MATLAB实现SVDD决策边界可视化

Self cultivation of Electronic Engineers - when a project is developed

Learn from Li Mu, deep learning - linear regression and basic optimization function
![[R language - basic drawing]](/img/1e/aebf1cbe02c4574671bac6dc2c9171.png)
[R language - basic drawing]

Module 8 of the construction camp

Introduction and advanced level of MySQL (II)
随机推荐
Bm11 list addition (II)
Software testing dry goods
R语言与数据分析实战11-数据的删除
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
BM16 delete duplicate elements in the ordered linked list -ii
VIM learning manual
Applet applet jump to official account page
用LEX(FLEX)生成PL语言的词法分析器
Is zero basic software testing training reliable?
Mongodb database replication table
Application of time series database in Hydropower Station
Is there any prospect and way out for software testing?
QT widget promoted to QWidget
Cv5200 wireless WiFi communication module, wireless video image transmission, real-time wireless communication technology
RFs self study notes (4): actual measurement model - the mixture of OK and CK, and then calculate the likelihood probability
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
Tikz draw Gantt chart in FJSP -trans necessary
Lookup - lookup of sequential table and ordered table
Time waits for no man. The rise of TSDB is at the right time
QT function optimization: QT 3D gallery