当前位置:网站首页>Self encapsulated database dbutils universal template
Self encapsulated database dbutils universal template
2022-07-26 10:13:00 【Yuyu fish】
List of articles
Preface
Many steps of database operation are repeated , It is not worth spending a lot of time and manpower to edit , So we can write a tool class by ourselves , When encapsulating several same methods of database operation , It's much more convenient to call directly . In short , To simplify the JDBC operation , Can write less code .
One 、 What are the main steps of encapsulating database operations ?
1. The import related jar package .
2. Create a profile , Go to the configuration file to edit the specific value of the database connection .
3. Write the loading and implementation configuration file in the tool class .
4. To realize the connection of database , close .
5. To implement transaction related operations , Open transaction , Commit transaction , Roll back the transaction .
Two 、 Use steps
1. Import and stock in
The code is as follows ( Example ):
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
2. Load configuration files and transaction information
The code is as follows ( Example ):
public class DbUtils {
private static DruidDataSource ds;
private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();
static {
Properties properties = new Properties();
InputStream is = DbUtils.class.getResourceAsStream("/database.properties");
try {
properties.load(is);
ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
3. The connection and closing methods of the database are encapsulated
// Database connection
public static Connection getConnection() {
Connection connection = THREAD_LOCAL.get();
try {
if (connection == null) {
connection = ds.getConnection();
THREAD_LOCAL.set(connection);
}
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// Database shutdown
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
THREAD_LOCAL.remove();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
4. Operations related to database transactions
// The beginning of the business
public static void begin() {
Connection connection = null;
try {
connection = getConnection();
connection.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
// Commit of transaction
public static void commit() {
Connection connection = null;
try {
connection = getConnection();
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(connection, null, null);
}
}
// Rollback of transaction
public static void rollback() {
Connection connection = null;
try {
connection = getConnection();
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(connection, null, null);
}
}
5. Database connection configuration information
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ems?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
# Initialize connection
initialSize=10
# Maximum number of connections
maxActive=30
# Minimum free connection
maxIdle=5
# Timeout waiting time
maxWait=3000
summary
If there are any mistakes , I hope you will point out that , Thank you for your care .
边栏推荐
- Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
- Distributed network communication framework: how to publish local services into RPC services
- 面试突击68:为什么 TCP 需要 3 次握手?
- IEEE conference upload font problem
- Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
- Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
- C language course design Tetris (Part 1)
- Learning about opencv (2)
- El table implements adding / deleting rows, and a parameter changes accordingly
- Why does new public chain Aptos meet market expectations?
猜你喜欢

Node memory overflow and V8 garbage collection mechanism

Learning about opencv (2)

SSG framework Gatsby accesses the database and displays it on the page

Leetcode 504. 七进制数

Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)

Distributed network communication framework: how to publish local services into RPC services

Leetcode 504. Hex number
![[fluorescent character effect]](/img/05/4f4c24c787881e073919b63a6864cf.jpg)
[fluorescent character effect]

Data communication foundation STP principle

万字详解“用知识图谱驱动企业业绩增长”
随机推荐
Opencv image processing
面试突击68:为什么 TCP 需要 3 次握手?
云原生(三十六) | Kubernetes篇之Harbor入门和安装
Show default image when wechat applet image cannot be displayed
Use of pclint in vs2013
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
Draw arrows with openlayer
MySQL function
[fluorescent character effect]
Installation and use of cocoapods
PMM (percona monitoring and management) installation record
Under win10 64 bit, matlab fails to configure notebook
Node memory overflow and V8 garbage collection mechanism
服务器内存故障预测居然可以这样做!
Explain automatic packing and unpacking?
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
Use of tabbarcontroller
The practice of OpenCV -- bank card number recognition
Flutter event distribution