Properties file not found
I'm following an online tutorial (MySQL Java tutorial) and I've just created a properties file to manage my mysql credentials. When I try to run the code below I can't a FileNotFoundException on "database.properties". It's located in my src folder so I don't know where the problem is? I'm using Eclipse IDE if that's relevant.
Thanks.
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class MySqlTest {
public static void main(String[] args) {
Connection con = null;
try {
con = getConnection();
Statement st = con.createStatement();
ResultSet result = st.executeQuery("SELECT * FROM Authors");
while (result.next()) {
System.out.print(result.getInt(1));
System.out.print(": ");
System.out.println(result.getString(2));
}
con.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public static Connection getConnection() throws FileNotFoundException,
IOException, SQLException {
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();
String url = props.getProperty("db.url");
String user = props.getProperty("db.user");
String passwd = props.getProperty("db.passwd");
return DriverManager.getConnection(url, user, passwd);
}
}