3

I have code of Connection class, where it gets a connection only from one database. Look at the DBConnection.java below

package eksim.db.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBConnection {

    public Connection conn=null;
    private static DBConnection dbConn=null;
    static final String DB_URL=
            "jdbc:sqlserver://10.0.0.47\\test;databaseName=a;";
    static final String DB_USER="sa";
    static final String DB_PASS="123";




    public DBConnection(){
         if(conn==null){
             try{
                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
                 conn=DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
             }
             catch(Exception ex){
                 ex.printStackTrace();
             }
         }
    }





    public static  DBConnection getInstance(){
        DBConnection conn=null;
        if(dbConn==null){
            System.out.println("New");
            dbConn=new DBConnection();
            conn=dbConn;
        } else{
            conn=dbConn;
        }
        return conn;
    }
    public Connection getCon() throws SQLException{
        return this.conn;
    }
}

Now, i want to create another connection into the different database. Could anyone give me suggestion to modify this code in order to get the result of Connection into 2 database? thanks for any reply

2 Answers 2

3

You need to provide some means to identify different connections, but also to configure those connections.

You could provide a newInstance method, passing it a name, which is used to identify the connection and the connection properties required to create that connection.

Then, when ever you wanted to get a reference to the connection, you could simply use getInstance(name)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class DBConnection {

    public Connection conn = null;

    private static Map<String, DBConnection> mapInstances = new HashMap<String, DBConnection>(25);

    private String dbURL;
    private String dbUser;
    private String dbPassword;

    private DBConnection(String dbURL, String dbUser, String dbPassword) {
        this.dbPassword = dbPassword;
        this.dbURL = dbURL;
        this.dbUser = dbUser;
    }

    public synchronized static DBConnection newInstance(String name, String dbURL, String dbUser, String dbPassword) {
        DBConnection con = new DBConnection(dbURL, dbUser, dbPassword);
        mapInstances.put(name, con);
        return con;
    }

    public synchronized static DBConnection getInstance(String name) {
        return mapInstances.get(name);
    }

    public synchronized Connection getCon() throws SQLException {
        if (conn == null) {
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
                conn = DriverManager.getConnection(dbURL, dbUser, dbPassword);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return this.conn;
    }

}

For example...

DBConnection db1 = DBConnection.newInstance("db1", ....);
DBConnection db2 = DBConnection.newInstance("db2", ....);

And when you want to access the connection again...

DBConnection db1 = DBConnection.getInstance("db1");
DBConnection db2 = DBConnection.getInstance("db2");
Sign up to request clarification or add additional context in comments.

2 Comments

think it would be best if you have ConcurrentHashMap instead of HashMap , its better while working with threads which is pretty much all the time :)
@AnanthaSharma It's certainly a valid point, but the two instance methods are synchronized
2
public class DBConnection {

public Connection conn=null;
private static DBConnection dbConn=null;
static final String DB_URL=
        "jdbc:sqlserver://10.0.0.47\\test;databaseName=a;";
static final String DB_USER="sa";
static final String DB_PASS="123";


 public Connection secCon=null;
static final String DB_URL_SECOND= // URL

static final String DB_USER_SECOND=""; // user name
static final String DB_PASS_SECOND="123";




public DBConnection(){
     if(conn==null){
         try{
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
             conn=DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
             secCon=DriverManager.getConnection(DB_URL_SECOND, DB_USER_SECOND, DB_PASS_SECOND);
         }
         catch(Exception ex){
             ex.printStackTrace();
         }
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.