为下面代码添加注释,public class DruidUtils { static DataSource dataSource; static { Properties properties=new Properties(); InputStream inputStream = JdbcTest.class.getClassLoader().getResourceAsStream("druid.properties"); try { properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } try { dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource() throws Exception { return dataSource; } }
时间: 2024-02-15 19:26:59 浏览: 73
/**
* DruidUtils是一个Druid连接池的工具类
*/
public class DruidUtils {
//声明一个静态的数据源对象
static DataSource dataSource;
//使用静态代码块初始化数据源
static {
//创建Properties对象
Properties properties=new Properties();
//获取配置文件
InputStream inputStream = JdbcTest.class.getClassLoader().getResourceAsStream("druid.properties");
try {
//加载配置文件
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
//创建数据源
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取数据源对象
* @return DataSource数据源对象
* @throws Exception
*/
public static DataSource getDataSource() throws Exception {
return dataSource;
}
}
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)