1.首先需要导入druid的jar包
下载方法见:数据库连接池druid 的jar包官网下载-最新版下载_时雨.`的博客-CSDN博客
2.再创建src下druid.properties文件(这里根据自己的数据库情况进行改动)
url=jdbc:mysql://localhost:3306/tb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username=root password=root driverClassName=com.mysql.cj.jdbc.Driver initialSize=10 maxActive=10
3.进行连接操作
//连接
private static DataSource source;
static {
try {
Properties pros = new Properties();
InputStream is =ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
pros.load(is);
source= DruidDataSourceFactory.createDataSource(pros);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection conn= source.getConnection();
return conn;
}