Click here to Skip to main content
12,957,007 members (57,620 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 1 May 2011

Java Properties Example using Singleton Pattern

, 1 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This post explains how to use properties in Java using singleton pattern


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, and store database information like the following:

# Static information
PORT 1433
USER test2
PASS test-2
JDBC jdbc:sqlserver

Now, we can use this file in singleton pattern:

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(){
        InputStream file = new FileInputStream(new File("")) ;
        Properties props = new Properties();
        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:

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 file.


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


About the Author

Green Mile
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.

You may also be interested in...


Comments and Discussions

Generalexcellent example Pin
Java Experience31-Jan-13 23:33
memberJava Experience31-Jan-13 23:33 
GeneralMy vote of 2 Pin
Nagy Vilmos2-May-11 3:43
memberNagy Vilmos2-May-11 3:43 
GeneralRe: My vote of 2 Pin
Green Mile2-May-11 4:23
memberGreen 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 1 May 2011
Article Copyright 2011 by Green Mile
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid