编写获取连接池对象工具类:
private static DruidDataSource druidDataSource= null;
/*放入静态代码块中,在调用静态方法getDataResource的时候触发类加载*/
static {
try {
/*从配置文件中获取*/
Properties properties = new Properties();
/*获取配置文件流对象*/
InputStream is = DbUtilsDataSource.class.getClassLoader().getResourceAsStream("com/xzq/druidutils/druidmess.properties");
/*加载配置文件*/
properties.load(is);
is.close();
/*利用工厂方式获取一个连接池对象*/
druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataResource(){
return druidDataSource;
}