I have a simple Java program that connects to a database. The connection is configured in db.properties
file. My program works fine.
When I want to compile it into a executable jar file, I will place the properties file in the same folder so user can easily edit the properties file and change the connection settings to their liking. The slight problem here is my dbpath
where I have to specify the exact directory to read the properties file.
I was wondering if I can save it in the same folder and set the path to current directory so no matter where the user save the folder, it will always be able to read the db properties without hardcoding the path.
Code:
Properties props=new Properties();
String dbpath = "C:\\Users\\nickywan123\\Documents\\db.properties";
FileInputStream in = new FileInputStream(dbpath);
props.load(in);
in.close();
String driver = props.getProperty("jdbc.driver");
if(driver!=null){
Class.forName(driver);
}
String url=props.getProperty("jdbc.url");
String username=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password");
Connection con = DriverManager.getConnection(url,username,password);
I've tried different solutions to get location of jar file such as Get location of JAR file but it doesn't return the location I want to be able to read it.
I appreciate any suggestion