package com.sd.utils;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class Druidutils {
//1加载配置文件,初始化连接池
//静态代码块,整个程序之执行一次
private static DruidDataSource dataSource;
static {
//1.1加载配置文件
Properties properties = new Properties();
InputStream is = Druidutils.class.getResourceAsStream("/database.properties");
try {
properties.load(is);
is.close();
//1.2初始化连接池
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch(Exception e){
e.printStackTrace();
}
}
//2返回连接池
public static DataSource getDataSource() {
return dataSource;
}
}