简体   繁体   English

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

[英]Java DataBase Connectivity Problem with MS SQL Server 2005 from a Remote Server

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: 我正在编写用于与MS SQL Server 2005连接的Java代码。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();
     }
}

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. 请给我您的宝贵建议,可以帮助我克服这个问题。

Make sure that your SQL Server is configured to use TCP/IP. 确保将您的SQL Server配置为使用TCP / IP。 Enable it from SQL Server's Network Utility app. 从SQL Server的Ne​​twork Utility应用程序启用它。 Also check there that the SQL Server is using port 1433 (IP Addresses - IPAll - TCP Port). 还要检查那里SQL Server是否使用端口1433(IP地址-IPAll-TCP端口)。

Try to use "telnet <server_host> 1433". 尝试使用“ telnet <server_host> 1433”。 If it doesn't connect you will not be able to establish a connection. 如果没有连接,您将无法建立连接。

ALLOW THE CONNECTION FIRST IN WHICH PC YOUR SQL SERVER IS RUNNING... GO TO CONTROL PANEL-->ADMIN. 允许在您的SQL Server正在运行的PC上首先进行连接...转到控制面板->管理。 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 工具-->具有高级安全性的Windows防火墙->入站规则->新规则->选择端口单选按钮->下一步->输入端口3306->单击下一步->最终给出规则名称像conn any ...单击完成

IMHO "Connection refused" means your database server is not visible from your application server. 恕我直言,“连接被拒绝”意味着您的数据库服务器从您的应用程序服务器中不可见。

  • Check IP address and port. 检查IP地址和端口。
  • Check database connectivity directly from your database server (to avoid firewalls). 直接从数据库服务器检查数据库连接(以避免防火墙)。
  • Check database connectivity from your application server. 从应用程序服务器检查数据库连接。

Hope this will help you 希望这个能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM