"Dbconnection.java" "db.properties"ファイルが見つかりません。 以下のファイルをどのように入手するかを示します。Java Fileinputstreamファイルが見つかりません
私の次のプロジェクトディレクトリ。
SRC
DB
- 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;
}
}
です。プロパティファイルをリソースフォルダに配置します。 – Abhishek
名前だけでなく、ファイルの絶対パスを使用してください。そうでなければ、.javaファイルを持つ場所にdb.propertiesを入れてください。 –
これまでに聞いたことのあるものはすべて無視し、 'db.properties'ファイルJARファイルにはまったく入りません。アプリケーションを実行するときに使用される現在の作業ディレクトリに置きます。 * Or *、JARファイルに入れ、 'FileInputStream'ではなく、リソースとしてロードします。 2007年以来 'Class.forName()'行は必要ではありませんでした。 – EJP