user10101
user10101

Reputation: 379

Use of Property file

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

Answers (2)

hovanessyan
hovanessyan

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:

  • using deployment tool to create packages, easy to deploy. For this I am using Maven, with a custom assembly descriptor which I provide to maven-assembly-plugin
  • such properties files as database configuration, in the project structure are located under /src/resources/config (for example)
  • in the assembly descriptor (which creates a zip file), all files from /src/resources/config are moved to the root of the zip in directory config - so in /config inside the zip file. /config should be added to classpath using maven-jar-plugin configuration for example, or the maven-dependency-plugin.
  • the main jar is located on the root level of the zip, and all other dependencies are in /lib folder (using the maven-dependency-plugin)

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

Stephen C
Stephen C

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:

  • telling the user to edit the properties file with a text editor,
  • coding your application's installer to ask the for the username and password and then insert them into the properties file at install time, or
  • providing a configuration tool or interface that allows the user to enter or update the properties.

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

Related Questions