public class JDBCUtil {
private static DataSource ds;
static {
try {
//加载配置文件
Properties pro = new Properties();
//使用classloader加载配置文件,获取字节输入流
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//初始化连接池对象
ds= DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接对象
public static DataSource getDataSource(){
return ds;
}
//获取连接connection对象
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
} |
|