“Dbconnection.java”“db.properties”文件找不到该文件。 我向您展示如何获取以下文件。
我的下一个项目目录。
源代码
数据库
- DbConnection.java
db.properties
DbConnection.java
public class DBConnection {
public static Connection getConnection() {
Properties props = new Properties();
FileInputStream fis = null;
Connection con = null;
try {
fis = new FileInputStream("db.properties");
props.load(fis);
out.println(props.getProperty("DB_DRIVER_CLASS"));
// load the Driver Class
Class.forName(props.getProperty("DB_DRIVER_CLASS"));
// create the connection now
con = DriverManager.getConnection(props.getProperty("DB_URL"),
props.getProperty("DB_USERNAME"),
props.getProperty("DB_PASSWORD"));
} catch (IOException | ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
}
最佳答案
fis = new FileInputStream("db.properties");
此行在运行应用程序的“主”目录下查找文件。它相当于“./db.properties”。如果您不知道应用程序的根目录,请尝试使用此行添加调试 new File(".").getAbsolutePath();然后将属性文件移到那里。
关于Java Fileinputstream 文件未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36999384/