Singleton is a class which has only one instance in whole application and provides a getInstance() method to access the singleton instance.
For example, you can use it to create a connection pool. It’s not wise to create a new connection every time a program needs to write something to a database; instead, a connection or a set of connections that are already a pool can be instantiated using the Singleton pattern.
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;
}
}