java properties example using singleton pattern

In java application, static data/information can be used in full application. Most of the “novice”/amateur developer as like me use hard code information into code, which is very dangerous for any project, specially client specific application. To escape 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.

At first we should create a file named like as db.properties, and can store database information like 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

import java.io.*;
import java.util.Properties;

public class DBInfo {
    static private DBInfo _instance = null;
    public String port = null;
    public String database = null;
    public String ip = null;
    public String user = null;
    public String pass = null;
    public String jdbc = null;
    public String driver = null;
    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 application

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

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

Advertisements

9 thoughts on “java properties example using singleton pattern”

  1. There’s no need of static properties here except for DBInfo _instance. The main purpose of using Singleton over static is to access the properties via instance. If you using static properties, then you can access them without instantiating the singleton class.

    1. @Amit: Thanks for your comment. You are right. In this post, I’ve tried to implement the “java properties using singleton pattern”. I think, I need to change the code.

      Again thanks for your comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s