1、概念
Druid(德鲁伊)连接池是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,是针对监控而生的DB连接池,据说是目前最好的连接池。
2、配置文件在src下创建一个druid.properties文件
参数含义可以参考博客:https://blog.csdn.net/weixin_39805338/article/details/81063493
jdbcUrl=jdbc:mysql://localhost:3306/jdbc
username=root
password=123456
initialSize=5
maxActive=15
minIdle=5
maxWait=3000
3、使用
Properties p = new Properties();
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties"));
//连接池对象
DataSource dataSource = new DruidDataSource();//import javax.sql.DataSource;
DruidDataSource ds = (DruidDataSource)dataSource;
//参数设置
ds.setUrl(p.getProperty("jdbcUrl"));
ds.setUsername(p.getProperty("username"));
ds.setPassword(p.getProperty("password"));
ds.setInitialSize(new Integer(p.getProperty("initialSize"