来自远程服务器的 MS SQL Server 2005 的 Java 数据库连接问题

发布于 2024-09-13 07:43:16 字数 2382 浏览 6 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风挽心 2024-09-20 07:43:16

确保您的 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.

才能让你更想念 2024-09-20 07:43:16

恕我直言,“连接被拒绝”意味着您的数据库服务器在应用程序服务器中不可见。

  • 检查 IP 地址和端口。
  • 直接从数据库服务器检查数据库连接(以避免防火墙)。
  • 检查应用程序服务器的数据库连接。

希望这会帮助你

IMHO "Connection refused" means your database server is not visible from your application server.

  • Check IP address and port.
  • Check database connectivity directly from your database server (to avoid firewalls).
  • Check database connectivity from your application server.

Hope this will help you

等待我真够勒 2024-09-20 07:43:16

首先允许连接您的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文