Wednesday, September 26, 2007

Usage of Properties file

This demonstrates the usage of Properties file where the commom values used through out the project can be stored.
-----------------------------------------------------------------------------------------------------------------

package com.mahesh.struts;

import java.util.ResourceBundle;

public class ResourceManager {
private static ResourceBundle bundle=ResourceBundle.getBundle("com.mahesh.struts.
ApplicationResources");


public static String getString(String key){
return bundle.getString(key)
}

}


com.mahesh.struts.ApplicationResources => This is a simple file having key-value pairs. The value can be retrieved from it using the key. The value can be retrieved using the "getString(key)" method.

The key-value pairs will be written in "com.mahesh.struts.ApplicationResources" is as shown below,
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.1.125:3306/
database.user=root
database.password=passwd

The values are retrieved using the key as shown below,

String driverClass = ResourceManager.getString("database.driver");

String dbUrl = ResourceManager.getString("database.url");

String dbUser = ResourceManager.getString("database.user");

String dbPassword = ResourceManager.getString("database.password");

No comments: