I am working on a webapp using Maven and Java servlets. I'm trying to build a connection to my local Postgres Database by retrieving its credentials from a Properties file because I don't want to hard code them. For this im using FileInputStream
in a "getProperties" general method. The problem is that i am using a Tomcat Server, so the project runs from the Tomcat directory, and I'm having trouble setting it so that my fellow programmers can run my app on their own machines without having to hardcode a directory. The final goal is to only have to change the file itself and never the code.
So i have a Properties file like this:
port = localhost
bdname = clothingdb
user = jmbs
pw = 123
and im trying to retrieve them like this:
String port = getProperties("port");
String bdname = getProperties("bdname");
String user = getProperties("user");
String pw = getProperties("pw");
Using this method:
public String getProperties(String propriedade)
{
String parametro = null;
try (InputStream input = new FileInputStream("svlocal.properties")) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
parametro = prop.getProperty(propriedade);
} catch (IOException ex) {
ex.printStackTrace();
}
return parametro;
}
For this i get the "File not found" error, because the project runs from the tomcat/bin
directory and not from the
NetBeansProjects/bookstore/
directory where i need to have the Properties file.
My actual question is, how can i set the Properties file, so that when i send my project to someone else he only has to run it and change the credentials on the Properties file to access his own local database, and not having to change directories or hard code them on the
getProperties()
method? Thanks in advance.