Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / Java

Java Properties Example using Singleton Pattern

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
1 May 2011CPOL 58.1K   6   3
This post explains how to use properties in Java using singleton pattern

Introduction

In a Java application, static data/information can be used in full application. Most of the "Beginner" developers like me use hard code information into code which is very dangerous for any project, especially client specific application. To prevent an unwanted disaster, we can use Java properties for static information and singleton pattern. Here, I am trying to figure out database information using Java properties and singleton pattern which can be used in full application.

Using the Code

At first, we should create a file named as db.properties, and store database information like the following:

# Static information
IP 000.000.0.00
PORT 1433
DATABASE test2
USER test2
PASS test-2
DRIVERNAME com.microsoft.sqlserver.jdbc.SQLServerDriver
JDBC jdbc:sqlserver
INSTANCE SQLEXPRESS

Now, we can use this file in singleton pattern:

Java
import java.io.*;
import java.util.Properties;
	 
public class DBInfo {
    static private DBInfo _instance = null;
    static public String port = null;
    static public String database = null;
    static public String ip = null;
    static public String user = null;
    static public String pass = null;
    static public String jdbc = null;
    static public String driver = null;
    static public String instance = null;
	 
    protected DBInfo(){
    try{
        InputStream file = new FileInputStream(new File("db.properties")) ;
        Properties props = new Properties();
        props.load(file);
        port = props.getProperty("PORT");
        ip = props.getProperty("IP");
        database = props.getProperty("DATABASE");
        user = props.getProperty("USER");
        pass = props.getProperty("PASS");
        jdbc = props.getProperty("JDBC");
        driver = props.getProperty("DRIVERNAME");
        instance = props.getProperty("INSTANCE");
       } 
    catch(Exception e){
        System.out.println("error" + e);
       }	 
    }
	 
    static public DBInfo instance(){
        if (_instance == null) {
            _instance = new DBInfo();
        }
        return _instance;
    }
}

Now, we can use this in an application:

Java
DBInfo dbInfo = DBInfo.instance();
String connString = dbInfo.jdbc + "://" + dbInfo.ip + "\\" + dbInfo.instance +":" + 
dbInfo.port + "; databaseName=" + dbInfo.database + "; userName=" + 
dbInfo.user +"; passWord="+ dbInfo.pass +";";

If the client want to change IP address or database port or something else, then client can do this just by changing the db.properties file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BRAC ICT
Bangladesh Bangladesh
Shaiful Islam(Palash) is dedicated, energetic, trusted, top-producing software Engineer with over 5+ years experience providing programming expertise. He is experienced in designing and developing Object Oriented solutions using Java/J2EE technology, SaaS application, SSO service, Mobile based App(Android), Travel & Courier application. He is the founder of V for Vagabond Lab. He started his career at Metafour Asia Ltd as a Software developer. Currently, He is working at BRAC ICT as senior software engineer.

Comments and Discussions

 
Generalexcellent example Pin
Java Experience31-Jan-13 23:33
Java Experience31-Jan-13 23:33 
GeneralMy vote of 2 Pin
Nagy Vilmos2-May-11 3:43
professionalNagy Vilmos2-May-11 3:43 
GeneralRe: My vote of 2 Pin
Green Mile2-May-11 4:23
Green Mile2-May-11 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.