当前位置:网站首页>Database connection pool Druid
Database connection pool Druid
2022-06-30 11:29:00 【TYUT ljk】
Druid
jar Package download
Enter official website https://repo1.maven.org/maven2/com/alibaba/druid/
Choose the version , Click enter 
choice jar package (.jar)
Implementation steps
- Import jar package
- Define configuration file
(1) yes properties Formal
(2) Can be called any name , It can be placed in any directory - Load profile Properties
- Get database connection pool : Get through the factory class DruidDataSourceFactory
- Get the connection :getConnection
The configuration file
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
package druid;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
public class DruidDemo {
public static void main(String[] args) throws Exception {
// Load profile
Properties pro = new Properties();
InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
// Get connection pool object
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
// Get the connection
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
Tool class
- Definition JDBCUtils
- Provide static code block loading configuration file , Initialize connection pool object
- Provide methods
(1) Get the connection method : Get the connection through the database connection pool
(2) Release resources
(3) Get connection pool
package druid.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
// Define member variables
private static DataSource ds;
static {
// Load profile
Properties pro = new Properties();
try {
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
// Get the connection
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
// Release resources
public static void close(Statement stmt, Connection conn){
if (stmt != null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement stmt, Connection conn){
if (rs != null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
close(stmt, conn);
}
// Get connection pool
public static DataSource getDataSource(){
return ds;
}
}
边栏推荐
- 以PolarDB为代表的阿里云数据库以跻身全球第一阵营
- Kotlin 协程调度切换线程是时候解开谜团了
- 数学(快速幂)
- Oracle netsuite helps TCM bio understand data changes and make business development more flexible
- EMC-浪涌
- dplyr 中的filter报错:Can‘t transform a data frame with duplicate names
- Oceanbase installation Yum source configuration error and Solutions
- 数据库 自动增长
- [leetcode 239] sliding window
- CVPR 2022 | 大幅减少零样本学习所需人工标注,马普所和北邮提出富含视觉信息的类别语义嵌入...
猜你喜欢

Digitalization is not a trial, but a wading out of "Xingzhi Digital China" × History of Foxconn

Methods and usage of promise async and await

国内首批!阿里云云原生数据湖产品通过信通院评测认证

科普达人丨漫画图解什么是eRDMA?

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information

R语言去重操作unique duplicate filter

启明星辰集团运维安全网关(堡垒机)再次夺得榜首!

Is the golden cycle of domestic databases coming?

微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供

Summer vacation study record
随机推荐
线代(高斯消元法、线性基)
语音识别-基础(一):简介【语音转文本】
对象映射 - Mapping.Mapster
科普达人丨漫画图解什么是eRDMA?
Record the memory leak of viewpager + recyclerview once
dplyr 中的filter报错:Can‘t transform a data frame with duplicate names
揭秘得物客服IM全链路通信过程
SQL必需掌握的100个重要知识点:插入数据
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
暑假学习记录
高通发布物联网案例集 “魔镜”、数字农业已经成为现实
8行代码实现快速排序,简单易懂图解!
SQL必需掌握的100个重要知识点:组合查询
Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
Win10 R package installation error: not installed in arch=i386
压缩状态DP位运算
The first China Digital Collection conference will be held soon
关于IP定位查询接口的测评Ⅲ
A theoretical defect of relative position coding transformer and Its Countermeasures