当前位置:网站首页>Create datasource using Druid connection pool
Create datasource using Druid connection pool
2022-07-27 05:12:00 【Autunomy】
Use Druid Connection pool creation DataSource( data source )
There are two ways to create
The first one is
Use Map aggregate , Set each attribute to create
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
public class Test01 {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap();
// Set up URL
map.put(DruidDataSourceFactory.PROP_URL, "jdbc:mysql://10.10.10.134:3306/java20");
// Set the drive Driver
map.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, "com.mysql.jdbc.Driver");
// Set the username
map.put(DruidDataSourceFactory.PROP_USERNAME,"root");
// Set the password
map.put(DruidDataSourceFactory.PROP_PASSWORD,"123456");
// create data source
DataSource dataSource = DruidDataSourceFactory.createDataSource(map);
}
}
The second kind
Use properties Configuration file
Create a... In the project root directory resources File and mark it as a resource directory
Create a db.properties And write the following configuration
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://10.10.10.134:3306/java20
username=root
password=123456
To write java class
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
public class Test02 {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(Test02.class.getClassLoader().getResourceAsStream("db.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
}
}
边栏推荐
- Customize the viewport height, and use scrolling for extra parts
- How to create an applet project
- 集合框架的使用
- kali系统arp介绍(断网嗅探密码抓包)
- What if Photoshop prompts that the temporary storage disk is full? How to solve the problem that PS temporary storage disk is full?
- [search] connectivity model of DFS + search order
- 文件处理(IO)
- The difference between strlen and sizeof
- 项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
- Event
猜你喜欢

Tcp server是如何一个端口处理多个客户端连接的(一对一还是一对多)

树莓派输出PWM波驱动舵机

Solution: how to use bash batch command in win10

Installation and template setting of integrated development environment pychar

【Acwing】第61场周赛 题解

How to test the payment process?

File dialog box

How to create an applet project

Translation of robot and precise vehicle localization based on multi sensor fusion in diverse city scenes

UUID and indexing rules
随机推荐
Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
使用ngrok做内网穿透
Li Kou achieved the second largest result
QT menu bar, toolbar and status bar
事件总结-常用总结
The difference between strlen and sizeof
Final Cut Pro Chinese tutorial (2) understanding of material window
Use ngrok for intranet penetration
Why is count (*) slow
QT 菜单栏、工具栏和状态栏
Be diligent in talking about what sidelines you can do now
对话框简介
动态内存函数的介绍(malloc free calloc realloc)
[untitled] I is circularly accumulated under certain conditions. The condition is usually the length of the loop array. When it exceeds the length, the loop will stop. Because the object cannot judge
树莓派输出PWM波驱动舵机
一、MySQL基础
MySQL storage engine and its differences
[acwing] solution to the 61st weekly match
智慧展厅设计的优势及适用行业分析
JVM上篇:内存与垃圾回收篇十--运行时数据区-直接内存