MS SQLServer2005に接続するためのJavaコードを記述しています。MSSQLServerはリモートサーバーのWindowsServer2003上にあります。次のコードを試していますが、接続を確立できません。
import java.*;
public class Connect {
private java.sql.Connection con = null;
private final String url = "jdbc:sqlserver://";
private final String serverName="xxx.xxx.xxx.xxx";
private final String portNumber = "1433";
private final String databaseName="myDb";
private final String userName ="user1";
private final String password = "xxxx";
private final String selectMethod = "cursor";
// Constructor
public Connect() {}
private String getConnectionUrl() {
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}
private java.sql.Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) System.out.println("Connection Successful!");
} catch(Exception e) {
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties() {
System.out.println("Perform Operations ");
}
private void closeConnection() {
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Connect myDbTest = new Connect();
// myDbTest.displayDbProperties();
}
}
しかし、私は次の例外を取得しています:
com.microsoft.sqlserver.jdbc.SQLServerException:ホストへのTCP/IP接続が失敗しました。java.net.ConnectException:接続が拒否されました:接続 com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(不明なソース)で getConnection()のエラートレース:ホストへのTCP/IP接続が失敗しました。java.net.ConnectException:接続が拒否されました:接続 エラー:アクティブな接続がありません
上記のコードのどこに問題があるのかわかりません。または、リモートサーバーに接続するために何らかの設定を行う必要がありますか。
この問題を克服するのに役立つ貴重な提案をください。