当前位置:网站首页>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;
}
}
边栏推荐
- Oracle netsuite helps TCM bio understand data changes and make business development more flexible
- 国产数据库的黄金周期要来了吗?
- LED driver library based on Hal Library
- The latest collection of arouter problems
- 8行代码实现快速排序,简单易懂图解!
- 数字化不是试出来,而是蹚出来的|行知数字中国 × 富士康史喆
- It's time for the kotlin coroutine to schedule thread switching to solve the mystery
- Esp32-c3 introductory tutorial basic part ⑫ - mass production burning device configuration and serial number, NVS partition confirmation, NVS partition generation program, CSV to bin
- 林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
- 14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
猜你喜欢

数据库连接池 druid

Oceanbase installation Yum source configuration error and Solutions

Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality

高通发布物联网案例集 “魔镜”、数字农业已经成为现实

以PolarDB为代表的阿里云数据库以跻身全球第一阵营

从开源项目探讨“FPGA挖矿”的本质

Wechat Emoji is written into the judgment, and every Emoji you send may become evidence in court

ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528

Alibaba cloud database represented by polardb ranks first in the world

The jetpack compose dropdownmenu is displayed following the finger click position
随机推荐
中移OneOS开发板学习入门
【IC5000教程】-01-使用daqIDEA图形化debug调试C代码
R language view version R package view version
数据库连接池 druid
SQL必需掌握的100个重要知识点:使用子查询
100 important knowledge points that SQL must master: summary data
8 lines of code to achieve quick sorting, easy to understand illustrations!
Esp32-c3 introductory tutorial basic part ⑫ - mass production burning device configuration and serial number, NVS partition confirmation, NVS partition generation program, CSV to bin
博弈论入门
TypeScript ReadonlyArray(只读数组类型) 详细介绍
Kongsong (ICT Institute) - cloud security capacity building and trend in the digital age
揭秘得物客服IM全链路通信过程
Is the golden cycle of domestic databases coming?
以PolarDB为代表的阿里云数据库以跻身全球第一阵营
[applet practice series] Introduction to the registration life cycle of the applet framework page
promise async和await的方法与使用
10 reasons for system performance failure
对象映射 - Mapping.Mapster
数字化不是试出来,而是蹚出来的|行知数字中国 × 富士康史喆
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大