当前位置:网站首页>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 .
边栏推荐
猜你喜欢
论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
Leetcode 504. 七进制数
服务发现原理分析与源码解读
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Introduction to latex, EPS picture bounding box
面试突击68:为什么 TCP 需要 3 次握手?
新建福厦铁路全线贯通 这将给福建沿海带来什么?
protobuf的基本用法
随机推荐
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Wechat applet learning notes 1
IEEE conference upload font problem
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
MySQL function
Uniapp error 7 < Map >: marker ID should be a number
Node memory overflow and V8 garbage collection mechanism
JS continuous assignment operation
万字详解“用知识图谱驱动企业业绩增长”
Jpg to EPS
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
Study notes of the first week of sophomore year
Flask框架初学-04-flask蓝图及代码抽离
Vs2019 configuring opencv
输入整数后输入整行字符串的解决方法
SQL Server 2008 R2 installation problems
解释一下自动装箱和自动拆箱?
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
protobuf的基本用法
Uni app learning summary