当前位置:网站首页>Druid 数据库连接池 详解
Druid 数据库连接池 详解
2022-07-06 11:22:00 【斯文~】
推荐阅读:JDBC详解
概述
1.数据库连接池是个容器,负责分配、管理数据库连接(Connection);
2.它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;
3.释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏;
可以类比线程池去理解。
优点
1.资源重用
2.提升系统响应速度
3.避免数据库连接遗漏

连接池是在一开始就创建好了一些连接(Connection)对象存储起来。用户需要连接数据库时,不需要自己创建连接,而只需要从连接池中获取一个连接进行使用,使用完毕后再将连接对象归还给连接池;这样就可以起到资源重用,也节省了频繁创建连接销毁连接所花费的时间,从而提升了系统响应的速度。
常见的数据库连接池: DBCP、C3P0、Druid
我们现在使用更多的是Druid,它的性能比其他两个会好一些。
Druid连接池是阿里巴巴开源的数据库连接池项目,功能强大,性能优秀,是Java语言最好的数据库连接池之一。
数据库连接池实现
标准接口:DataSource
官方(SUN) 提供的数据库连接池标准接口,由第三方组织实现此接口。该接口提供了获取连接的功能:Connection getConnection()
那么以后就不需要通过 DriverManager 对象获取 Connection 对象,而是通过连接池(DataSource)获取 Connection 对象。
Driud使用流程
1.导入jar包 druid-1.1.12.jar
2.定义配置文件 druid.properties
3.加载配置文件
4.获取数据库连接池对象
5.获取连接

代码示例
配置文件druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/sd?useSSL=false&useServerPrepStmts=true
username=root
password=root
# 初始连接数
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
Java代码
public static void main(String[] args) throws Exception {
//1.导入jar包
//2.定义配置文件
//3. 加载配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("C:\\Users\\斯文\\Desktop\\Java\\demo\\index\\src\\druid.properties"));
//4. 获取连接池对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//5. 获取数据库连接 Connection
Connection conn=dataSource.getConnection();
//6.操作数据库
String sql = "select * from st";
Statement stmt=conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(id + " " + name + " " + age);
}
}
推荐阅读:JDBC详解
边栏推荐
- 驼峰式与下划线命名规则(Camel case With hungarian notation)
- Oracle advanced (IV) table connection explanation
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- PMP每日一练 | 考试不迷路-7.6
- Summary of performance knowledge points
- 使用map函数、split函数一行键入多个元素
- 能源行业的数字化“新”运维
- Synchronous development of business and application: strategic suggestions for application modernization
- Multithreading Basics: basic concepts of threads and creation of threads
- Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
猜你喜欢

Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries

Binary search tree

LeetCode-1279. 红绿灯路口
Interview assault 63: how to remove duplication in MySQL?

How to type multiple spaces when editing CSDN articles

openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度

Multithreading Basics: basic concepts of threads and creation of threads

Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services

多线程基础:线程基本概念与线程的创建

Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
随机推荐
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
ROS自定义消息发布订阅示例
How to type multiple spaces when editing CSDN articles
php+redis实现超时取消订单功能
A wearable arm device for night and sleeveless blood pressure measurement [translation]
test about BinaryTree
Take a look at how cabloyjs workflow engine implements activiti boundary events
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
ROS custom message publishing subscription example
Camel case with Hungarian notation
[matlab] Simulink the input and output variables of the same module cannot have the same name
On AAE
Jushan database was among the first batch of financial information innovation solutions!
能源行业的数字化“新”运维
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
如何提高网站权重
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip