我有Connection类的代码,它只从一个数据库获取连接。查看下面的DBConnection.java
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;
}
}
现在,我想创建另一个连接到不同的数据库。任何人都可以给我建议修改这段代码,以便将Connection的结果输入2个数据库吗?谢谢你的回复
答案 0 :(得分:3)
您需要提供一些方法来识别不同的连接,还需要配置这些连接。
您可以提供newInstance
方法,并为其传递一个名称,该名称用于标识创建该连接所需的连接和连接属性。
然后,当您想要获得对该连接的引用时,您只需使用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;
}
}
例如......
DBConnection db1 = DBConnection.newInstance("db1", ....);
DBConnection db2 = DBConnection.newInstance("db2", ....);
当您想再次访问连接时......
DBConnection db1 = DBConnection.getInstance("db1");
DBConnection db2 = DBConnection.getInstance("db2");
答案 1 :(得分: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();
}
}
}