1.
package com.cuc.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtil {
private static DataSource ds;
static {
try {
//获取配置路径
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
//加载配置文件
Properties pro = new Properties();
pro.load(is);
//初始化连接对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接对象
*/
public static DataSource getDataSource() {
return ds;
}
/**
* 获取连接池
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
getResourceAsStream("druid.properties");默认则是从ClassPath根下获取,path不能以’/'开头,ClassPath一般在WEB-INF\classes下
2.druid.properties配置文件放在src根目录