今天主要讲了DBCP连接池,写了一个工具类,获取数据源,把QueryRunner的使用又完善了一下:
public class DataSourceUtils {
private static String driverClass;
private static String url;
private static String user;
private static String password;
private static BasicDataSource datasource;
private DataSourceUtils(){}
static{
try {
//读文件
readConfig();
datasource = new BasicDataSource();
datasource.setDriverClassName(driverClass);
datasource.setUrl(url);
datasource.setUsername(user);
datasource.setPassword(password);
//设置连接数
datasource.setInitialSize(10);
datasource.setMaxActive(8);
datasource.setMaxIdle(5);
datasource.setMinIdle(2);
} catch (IOException e) {
e.printStackTrace();
}
}
public static DataSource getDataSource(){
return datasource;
}
private static void readConfig() throws IOException {
// 类加载获取配置文件的输入流
InputStream is = DataSourceUtils.class.getClassLoader().getResourceAsStream("database.properties");
Properties pro = new Properties();
// 读取配置文件
pro.load(is);
// 获取参数
driverClass = pro.getProperty("driverClass");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
}
元旦快乐!明年见...
|
|