读取配置文件失败(文件读取失败是什么情况)
一开始是用jdbc读取外部的数据库配置文件,文件如下:
packagehanxiu.util;
importjava.io.inputstream;
importjava.sql.*;
importjava.util.properties;
/**
*jdbc工具类:
*1)获取connection
*2)释放资源
*/
publicclassjdbcutil{
/**
*获取connection
*@return所获得到的jdbc的connection
*/
publicstaticconnectiongetconnection()throwsexception{
/**
*不建议大家把配置硬编码到代码中
*
*最佳实践:配置性的建议写到配置文件中
//stringurl="jdbc:mysql:///springdata";
//stringuser="root";
//stringpassword="123";
//stringdriverclass="com.mysql.jdbc.driver";
inputstreaminputstream=jdbcutil.class.getclassloader().getresourceasstream("db.properties");
propertiesproperties=newproperties();
system.out.print(inputstream);
properties.load(inputstream);
stringurl=properties.getproperty("jdbc.url");
stringuser=properties.getproperty("jdbc.user");
stringpassword=properties.getproperty("jdbc.password");
stringdriverclass=properties.getproperty("jdbc.driverclass");
class.forname(driverclass);
connectionconnection=drivermanager.getconnection(url,user,password);
returnconnection;
}
/**
*释放db相关的资源
*@paramresultset
*@paramstatement
*@paramconnection
*/
publicstaticvoidrelease(resultsetresultset,
statementstatement,
connectionconnection){
if(resultset!=null){
try{
resultset.close();
}catch(sqlexceptione){
e.printstacktrace();
}
}
if(statement!=null){
try{
statement.close();
}catch(sqlexceptione){
e.printstacktrace();
}
}
if(connection!=null){
try{
connection.close();
}catch(sqlexceptione){
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,报的错误如下
后面通过找资料观查发现才知道文件夹的类型没设置,通过这样设置可以解决问题