当前位置:网站首页>Introduction to JDBC (IV) - use of Druid connection pool
Introduction to JDBC (IV) - use of Druid connection pool
2022-06-23 05:24:00 【The silent Lord returns to the sand】
1.Druid Connection pool ( a key )
Connection pool :
summary : Put multiple connection objects in the pool in advance , When a user uses a connection object , Take it out of the pool ; When it is used up, it is recycled to the pool
benefits : Reduce the number of connection objects created and destroyed , Improved performance
principle :( Reuse mechanism )
Put... In the set 10 A connection object
If a user uses a connection object , Get... From the collection , And delete the objects in the collection
If the execution is complete , call close, Recycle resources ( Connect objects , Add back to the collection , Reuse for another user )
Druid Connection pool : It is provided by Alibaba , Recognized as the best performance connection pool product ; Other -c3p0,dbcp
Use steps :
1. Import Druid The connection pool jar package
2. To write Druid Configuration file for db.properties;( Be careful :key Fix , Value can be changed )
3. Load profile , Acquisition data source , To get the connection object
4.close It has become recycling ( There are rewrites in the connection pool )
---------------db.properties To configure ---------------
# connections setting up
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb1
username=root
password=123
#<!-- Initialize connection -->
initialSize=10
# Maximum number of connections
maxActive=50
#<!-- Minimum free connection -->
minIdle=5
#<!-- Timeout wait time in milliseconds 60000 millisecond /1000 be equal to 60 second -->
maxWait=5000
------------DBUtils Connection pool tool class -------------
public class DBUtils {
private static DataSource dataSource;
private static Properties p = new Properties();
// Static code block : Load only once
static{
// Reflection object calls getResourceAsStream
// from src Get... From the directory db.properties Resources for
try {
InputStream is = DBUtils.class.getResourceAsStream("/db.properties");
p.load(is); // Load connection pool configuration
// According to the incoming configuration , Acquisition data source , The data source contains the operation of connection pool
dataSource = DruidDataSourceFactory.createDataSource(p);
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource(){
return dataSource; // Acquisition data source
}
public static Connection getConnection(){
Connection conn = null;
try {
// Get the connection object from the connection pool
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
2.DaoUtils The third party of jar
DaoUtils The third party jar Package introduction
Internal implementation : And we wrote it ourselves DaoUtils It's similar , Provides , We don't have to write it ourselves
A third party jar Division level made crud Other than sql Encapsulation of statements ; for example : polymerization , Link query
step :
1. Import dbutils Of jar package (DaoUtils The implementation of the )
2. adopt QueryRunner call update and query Method to add, delete, modify and query
public class PersonDaoImpl {
// Use QueryRunner Realize the function of adding, deleting, modifying and checking
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
public int insert(Person p) {
String sql = "insert into person(name,age,bornDate,email,address) values(?,?,?,?,?)";
try {
return queryRunner.update(sql,p.getName(),p.getAge(),p.getBornDate(),p.getEmail(),p.getAddress());
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public int update(Person p) {
String sql = "update person set name=?,age=? where id=?";
try {
return queryRunner.update(sql,p.getName(),p.getAge(),p.getId());
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public int delete(int id) {
String sql = "delete from person where id=?";
try {
return queryRunner.update(sql,id);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public Person selectById(int id) {
String sql = "select * from person where id=?";
try {
return queryRunner.query(sql, new BeanHandler<>(Person.class), id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List<Person> selectAll() {
String sql = "select * from person";
try {
return queryRunner.query(sql,new BeanListHandler<>(Person.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
边栏推荐
- MMDeploy快速安装及使用说明
- MCS: continuous random variable chi square distribution
- MCS: discrete random variable
- C'est dur de trouver un emploi? Ali des trois côtés, heureusement qu'il s'est bien préparé et qu'il a pris un produit.
- Spark 离线开发框架设计与实现
- OSPF shunt test
- 投资风险管理
- [microservices | Nacos] list of issues related to the Nacos version
- Chapter IX app project test (1)
- 架构师之路,从「存储选型」起步
猜你喜欢

Arduino flame sensor (with code)

When SBAS encounters rtklib

Event log keyword: eventlogtags logtags

导出带水印的PDF

Web 应用程序安全测试指南

网上有真实的兼职吗?大学生怎么找暑期兼职?

【opencv450】帧间差分法

树莓派网络远程访问

The propeller framework v2.3 releases the highly reusable operator library Phi! Restructure development paradigm to reduce cost and increase efficiency

Difficult to find a job in a bad environment? Ali on three sides. Fortunately, he has made full preparations and has offered
随机推荐
Onnxoptimizer, onnxsim usage records
104. 简易聊天室7:使用 Socket 传递对象
Ams:startactivity desktop launch application
大環境不好難找工作?三面阿裏,幸好做足了准備,已拿offer
konva 系列教程 1:konva 是什么?
99 multiplication table bat
网上有真实的兼职吗?大学生怎么找暑期兼职?
618 how to break through the siege? Haier Zhijia: do a good job in digitalization of users
Three tier architecture experiment
奇门遁甲辅助决策软件
计算欧式距离和余弦相似度
气象绘图软件Panoply使用教程 (不定时更新)
PHP move_ uploaded_ File failed to upload mobile pictures
Event日志关键字:EventLogTags.logtags
Mongodb sharding principle
OSPF shunt test
MCS: discrete random variable - uniform distribution
Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
APP自动化测试-Appium进阶
牛B程序员在“创建索引”时都会注意啥?