来自远程服务器的 MS SQL Server 2005 的 Java 数据库连接问题
我正在编写一个 java 代码来连接 MS SQL Server 2005。MS SQL Server 位于远程服务器 windows server 2003 上。我正在尝试以下代码,但无法建立连接:
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: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source) Error Trace in getConnection() : The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect Error: No active Connection
我没有上面代码中的问题出在哪里还是我需要做一些设置 连接到远程服务器。
请给我您宝贵的建议,这可以帮助我克服这个问题。
I am writing a java code to connect with MS SQL Server 2005. MS SQL Server is on Remote server windows server 2003. I am trying the following code but i am unable to establish a connection:
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();
}
}
But I am getting following exceptions:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source) Error Trace in getConnection() : The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect Error: No active Connection
I am not getting where is the problem in the above code or do i need to do some setting
to connect to remote server.
Please give me your valuable suggestion which can help me to overcome with this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的 SQL Server 配置为使用 TCP/IP。从 SQL Server 的网络实用程序应用程序启用它。还要检查 SQL Server 是否正在使用端口 1433(IP 地址 - IPAll - TCP 端口)。
尝试使用“telnet1433”。如果未连接,您将无法建立连接。
Make sure that your SQL Server is configured to use TCP/IP. Enable it from SQL Server's Network Utility app. Also check there that the SQL Server is using port 1433 (IP Addresses - IPAll - TCP Port).
Try to use "telnet <server_host> 1433". If it doesn't connect you will not be able to establish a connection.
恕我直言,“连接被拒绝”意味着您的数据库服务器在应用程序服务器中不可见。
希望这会帮助你
IMHO "Connection refused" means your database server is not visible from your application server.
Hope this will help you
首先允许连接您的 SQL Server 正在运行的 PC...
转到控制面板-->管理。工具--->高级安全Windows防火墙-->入站规则-->新规则-->选择端口单选按钮-->下一步-->输入端口3306-->单击下一步-- ->最后给出规则名称,如 conn any...单击完成
ALLOW THE CONNECTION FIRST IN WHICH PC YOUR SQL SERVER IS RUNNING...
GO TO CONTROL PANEL-->ADMIN. TOOLS--->Windows Firewall with Advanced Security-->Inbounded rules-->new rule-->select port radio button -->next-->enter port 3306-->click next -->finally give the rule name like conn any...click finish