当前位置:网站首页>数据库连接池
数据库连接池
2022-07-21 07:03:00 【路过Coder】
Druid
Druid是阿里巴巴开源连接池组件,是最好的连接池之一
Druid对数据库连接进行有效管理与重用,最大化程序执行效率
连接池负责创建管理连接程序只负责取用与归还
Druid连接池的配置与使用
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
创建属性文件
druid-config.properties
# 配置数据库的连接参数
driverClassName= com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/servicecenter?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=root
# 配置连接池的参数
initialSize=5 //连接池准备好之后会默认地创建5个数据库连接
maxActive=10 //若5个链接不够用,那么由连接池创建新的连接,但是总数不超过10
maxWait=3000
maxIdle=6
minIdle=3
创建类
DruidSample
public class DruidSample {
public static void main(String[] args) {
//1.加载属性文件
Properties properties = new Properties();
String propertyFile = DruidSample.class.getResource("druid-config.properties").getPath();
//路径中 空格->%20 C: \java code\druid-config.properties
//c:\java%20code\druid-config.properties
try {
propertyFile = new URLDecoder().decode(propertyFile,"UTF-8");
} catch (UnsupportedEncodingException e) { //捕捉不支持的编码异常
e.printStackTrace();
}
try {
properties.load(new FileInputStream(propertyFile));
} catch (Exception e) {
e.printStackTrace();
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//2.获取DataSource数据源对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//3.创建数据库连接
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("selete * from employee limit 0,10");
rs = pstmt.executeQuery();
while (rs.next()) {
Integer empId = rs.getInt(1) ;
String ename = rs.getString ("ename") ;
String dname = rs.getString ("dname") ;
Float salary = rs.getFloat ("salary") ;
System.out.println(dname + "-" + empId + "-" + ename + "-" + salary);
} catch (Exception e) {
e.printStackTrace();
}finally {
DbUtils.closeConnection(rs,pstmt,conn);
}
}
}
C3P0连接池
c3p0-config.xml
<?xml version= ="1.0" encoding ="UTF-8" ?>
<c3p0-config>
<default- config>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/servicecenter?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
<property name="user"> root</property>
<property name="password">root</property>
<!-- 连接池初始连接数量-->
<property name="initialPoolsize">10</property>
<!--最大连接数量-->
<property name="maxPoolsize">20</property>
</default-config>
</c3p0-config>
C3P0Sample
public class C3P0Sample {
public static void main(String[] args) {
//1.加载属性文件
//2.获取DataSource数据源对象
DataSource dataSource = ComboPooledDataSource();
//3.创建数据库连接
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("selete * from employee limit 0,10");
rs = pstmt.executeQuery();
while (rs.next()) {
Integer empId = rs.getInt(1) ;
String ename = rs.getString ("ename") ;
String dname = rs.getString ("dname") ;
Float salary = rs.getFloat ("salary") ;
System.out.println(dname + "-" + empId + "-" + ename + "-" + salary);
} catch (Exception e) {
e.printStackTrace();
}finally {
DbUtils.closeConnection(rs,pstmt,conn);
}
}
}
边栏推荐
猜你喜欢
Malloc et configurateur d'espace
New features of mqtt5.0 (compare with 3.1.1)
44: Chapter 4: develop file service: 5: integrate fastdfs in [files] file service to realize the logic of [upload avatar]; (including: integrating fastdfs in the project; cross domain issues; creating
The sandbox teamed up with agoria to build agoriaverse
第一讲 概述
Niu Ke brushes question 01 - Kiki de duplication of integers and sorting (C language)
Word 2016 strange problem: file save error copy crash
vector的常见接口介绍
百度工程师眼中的云原生可观测性追踪技术
[rm_ee_note] 1 gm6020 transceiver & simple PID debugging
随机推荐
Can multithreading optimize program performance?
Diffusion Model
Carefree Manager installation
[rm_ee_note] 1 gm6020 transceiver & simple PID debugging
44: Chapter 4: develop file service: 5: integrate fastdfs in [files] file service to realize the logic of [upload avatar]; (including: integrating fastdfs in the project; cross domain issues; creating
“我放弃编程,写了一本130万字的硬科幻小说”
Cannot open source file < stdio. in CUDA file h>
Check the version number of IAR project
weirdo The interview topics include toilet habits, eating time and sleeping time
接口测试中,要验证和使用数据库吗
【安全狗漏洞通告】Apache Spark 命令注入漏洞方案
Markdown 转 PDF API 数据接口
Qt:qstring and qstringlist
VScode运行C语言文件
音频自动增益控制 AGC 解决的问题及原理解析
【日常训练】1260. 二维网格迁移
JS get the server IP, port and protocol
Diversified system to carry out maker education activities
LeetCode 1911. 最大子序列交替和
Malloc and space Configurator