当前位置:网站首页>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);
}
}
}
边栏推荐
- How to adjust the brightness of win11? Four methods of adjusting screen brightness in win11
- What does real HTAP mean to users and developers?
- How does the mqtt server built with emqx forward data and save it to the cloud database?
- Pytest custom hook function
- How many of the top ten test tools in 2022 do you master
- 2022年暑假ACM热身练习3(详细)
- Is the prospect of software testing dead? Has it entered the cold winter?
- UWB module realizes personnel precise positioning, ultra wideband pulse technology scheme, and real-time centimeter level positioning application
- JDBC简单封装
- SQL custom automatic calculation
猜你喜欢

3、 Uni app fixed or direct to a certain page

优麒麟系统安装BeyondComare

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

BM14 链表的奇偶重排

From Bayesian filter to Kalman filter (2)

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

Interpretation of ue4.25 slate source code

Structure and working principle of thyristor

UWB module realizes personnel precise positioning, ultra wideband pulse technology scheme, and real-time centimeter level positioning application
![[R language - basic drawing]](/img/1e/aebf1cbe02c4574671bac6dc2c9171.png)
[R language - basic drawing]
随机推荐
FTM module of K60: configure motor, encoder and steering gear
pytest 自定义HOOK函数
Is there a future for changing careers in learning software testing?
[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code
What does real HTAP mean to users and developers?
[physical application] atmospheric absorption loss with matlab code
Is the software testing training institution reliable?
When unity customizes the editor, let the subclass inherit the inspector display effect of the parent class
一家芯片公司倒在了B轮
架构实战营第8模块作业
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
[operation] differences between Oracle, MySQL and sqlserver
How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
Creating new projects and adding your own programs
DevCon. Exe export output to the specified file
Easynlp Chinese text and image generation model takes you to become an artist in seconds
RTC clock: a calendar
Pytest custom hook function
QT with line encoding output cout
2022年最火的十大测试工具,你掌握了几个