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

Passing parameter from one template to another template in xsl

Passing parameter from one template to another template in xsl is given below:
Using with parameter tag it is possible to pass parameter one template to another.

<xsl:template match="Address">
  <xsl:apply-templates select="./Name" >
    <xsl:with-param name="N1" select="urlD:decode(N1)"></xsl:with-param>
  </xsl:apply-templates>
</xsl:template>

Now Get this ‘N1’ parameter in another template, and use it

<xsl:template match="Name">
  <xsl:param name="N1"></xsl:param>
</xsl:template>

How Facebook Ships Code (via FrameThink – Frameworks for Thinking People)

cool. It’s realy cool.
What do you think? Would “developer-driven culture” work at your company?
FaceBook follows this culture to make billion billion doller. wow!

I'm fascinated by the way Facebook operates.  It's a very unique environment, not easily replicated (nor would their system work for all companies, even if they tried).  These are notes gathered from talking with many friends at Facebook about how the company develops and releases software. Seems like others are also interested in Facebook…   The company's developer-driven culture is coming under greater public scrutiny and other companies are … Read More

via FrameThink – Frameworks for Thinking People