连接池
package com.wang.druid;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.util.Collections;
import java.util.Properties;
public class druidjdbc {
public static void main(String[] args) throws Exception {
Properties properties=new Properties();
properties.load(new FileInputStream("src/Druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection=dataSource.getConnection();
System.out.println(connection);
}
}
首先手写Druid配置文件,一定要先加Druid的jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
配置文件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
username=root
password=wjs790929
initialSize=5
maxActive=10
maxWait=3000
新建Properties对象,调用load方法来加载配置(路径要写好)
创建连接池导入配置
最后创建连接
之后步骤和之前一样