读取配置文件失败(文件读取失败是什么情况)
读取配置文件失败(文件读取失败是什么情况)
一开始是用JDBC读取外部的数据库配置文件,文件如下:
package hanxiu.util;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* JDBC工具类:
* 1) 获取Connection
* 2) 释放资源
*/
public class JDBCUtil {
/**
* 获取Connection
* @return 所获得到的JDBC的Connection
*/
public static Connection getConnection() throws Exception {
/**
* 不建议大家把配置硬编码到代码中
*
* 最佳实践:配置性的建议写到配置文件中
// String url = "jdbc:mysql:///springdata";
// String user = "root";
// String password = "123";
// String driverClass = "com.mysql.jdbc.Driver";
InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
System.out.print(inputStream);
properties.load(inputStream);
String url = properties.getProperty("jdbc.url");
String user = properties.getProperty("jdbc.user");
String password = properties.getProperty("jdbc.password");
String driverClass = properties.getProperty("jdbc.driverClass");
Class.forName(driverClass);
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}
/**
* 释放DB相关的资源
* @param resultSet
* @param statement
* @param connection
*/
public static void release(ResultSet resultSet,
Statement statement,
Connection connection){
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
一直无法读取到db.properties,报的错误如下
后面通过找资料观查发现才知道文件夹的类型没设置,通过这样设置可以解决问题
相关推荐
- 湖北三江航天建筑工程有限公司以 60925996.99 元中标红林总装厂房二期工程
- 江西省天久地矿建设集团有限公司中标龙里县城区排涝工程勘测
- 北京中和联信供应链管理有限公司中标山地农业科技创新基地植物表型研究设备采购及伴随服务(重新招标)项目,中标金额 7764000 元
- 霸州市佳理鑫五金制品厂中标新乐市第三中学采购项目
- 河北泽辉市政工程有限公司等为路南区乡村振兴环境综合治理项目(一期)一标段工程总承包(EPC)(二次)中标候选人
- 河北石府建设工程有限公司10110736.93元中标高铁片区景观提升项目施工三标段
- 中基恒源建设有限公司中标高铁片区(含新华商业广场)景观提升项目施工五标段,中标价 13430852.95 元
- 九芝堂换帅完成工商变更
- 山西建设投资集团有限公司为大宁县水果供应链基地运营配套建设项目施工(二次)第一中标候选人
- 浙江宁慈建设工程有限公司以97028327元中标慈溪市城市生活垃圾收转运一体化建设项目(一期)
