Reputation: 379
I am creating properties file for database connection..I have given the path of the file and I am storing properties using the function:
public void setProperty(String key, String value) {
properties.setProperty(key, value);
}
I have set the properties like:
"url","jdbc:mysql://localhost:3306/";"dbname","example";"user", "postgres";"Pass","123";
My question is when I am using my application in another system how could this properties file is useful (Actually I have gone through the "property file in java" tutorials but I was unable to understand how they are used).
In new system user name and password would be different,so how could this file be useful??
Upvotes: 6
Views: 1582
Reputation: 31483
At least to my understanding the idea of property files are to be outside of the jar file. I will describe my way to go - the basic idea is to use relative paths and always the same structure when you package your application:
This way you ship a single zip file, which has inside structure like:
/
/config
/bin
/lib
<main jar file with manifest - the manifest contains all the classpath info - this info is automatically generated by the maven plugins>
When the credentials change, all you have to do is edit the properties file. Another matter is, if that's secure.
Upvotes: 0
Reputation: 719661
In new system user name and password would be different,so how could this file be useful??
It is useful precisely because the file can be different on different systems. By putting the system-specific details into a file that can be changed on different systems, you avoid having to write different Java code for each different system.
But how could I know the other users user name and password
In the scenario where you don't know, you have a number of alternatives, such as:
By the way, it looks like you have invented a custom property file format. You didn't need to do that. The java.util.Properties
class offers two standard formats and methods for reading and writing them.
Upvotes: 4