我创建了一个 dbConnection.properties 文件并将其放入 WEB-INF。为了读取该文件并连接到数据库,我编写了一个类作为
public class DbConnection {
public static Connection connection()
{
Connection con=null;
//String connectionURL = "jdbc:mysql://localhost:3306/demo";
try
{
Properties prop=new Properties();
FileInputStream in = new FileInputStream(System.getProperty("WEB-INF/dbConnection.properties"));
prop.load(in);
in.close();
String drivers = prop.getProperty("jdbc.drivers");
String connectionURL = prop.getProperty("jdbc.url");
String username = prop.getProperty("jdbc.username");
String password = prop.getProperty("jdbc.password");
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName(drivers);
con=DriverManager.getConnection(connectionURL,username,password);
System.out.println("Connection Successful");
return con;
}
catch(Exception e)
{
System.out.println("error !!");
}
return null;
}
}
它进入 catch 块。我如何读取 .properties 文件?