2012-11-08 84 views
1

Iam試圖從系統訪問數據庫到其他系統,下面是代碼。如何使用java訪問遠程數據庫

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 


public class testConnection { 

    /** 
    * @param args 
    * @throws SQLException 
    */ 
    public static void main(String[] args) throws SQLException { 
     // TODO Auto-generated method stub 
     String connectionURL = "jdbc:sqlserver://xxx.xxx.xx.xx:1433;DatabaseName=fingerprintdb;user=sa;password=test"; 
     // Change the connection string according to your db, ip, username and password 
      Connection con = null; 
     try { 

      // Load the Driver class. 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      // If you are using any other database then load the right driver here. 

      //Create the connection using the static getConnection method 
      con = DriverManager.getConnection (connectionURL); 

      //Create a Statement class to execute the SQL statement 
      Statement stmt = con.createStatement(); 

      //Execute the SQL statement and get the results in a Resultset 
      ResultSet rs = stmt.executeQuery("select moviename, releasedate from movies"); 

      // Iterate through the ResultSet, displaying two values 
      // for each row using the getString method 

      while (rs.next()) 
       System.out.println("Name= " + rs.getString("moviename") + " Date= " + rs.getString("releasedate")); 
     } 
     catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     finally { 
      // Close the connection 
      con.close(); 
     } 

    } 

} 

以下是IAM取得的異常。

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host xxx.xxx.xx.xx, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.". 
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716) 
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at testConnection.main(testConnection.java:26) 
Exception in thread "main" java.lang.NullPointerException 
    at testConnection.main(testConnection.java:48) 

Iam能夠從同一系統連接到數據庫。但現在我試圖連接遠程方式。

我從cmd提示符中輸入ipconfig得到了上述ip地址。 我是否正確,如果不是,請糾正我。

以下是對主機的ping記錄。

Pinging xxx.xxx.xx.xx with 32 bytes of data: 

Reply from xxx.xxx.xx.xx: TTL expired in transit. 
Reply from xxx.xxx.xx.xx: TTL expired in transit. 
Reply from xxx.xxx.xx.xx: TTL expired in transit. 
Reply from xxx.xxx.xx.xx: TTL expired in transit. 

Ping statistics for xxx.xxx.xx.xx: 
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 
Approximate round trip times in milli-seconds: 
    Minimum = 0ms, Maximum = 0ms, Average = 0ms 
+0

您是否ping過主機? –

+0

@喬伊請檢查我的更新 – developer

+0

感謝您的更新。您是否嘗試在該端口上打開telnet連接(1433)?只是爲了檢查特定的端口是否打開? –

回答

1

是的,有時會發生.. (請在安裝SQLServer的你下面的過程)

你需要開啓時進入到去

Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager

SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLExpress 

在哪裏可以找到協議TCP/IP,如果禁用,那麼啓用它點擊TCP/IP,你會發現它的屬性。

在此屬性中刪除所有TCP動態端口添加的價值1433的所有TCP端口 並重新啓動

SQL Server Services > SQL Server 

及其做...

+0

我會嘗試你的建議,並會讓你知道狀態 – developer

+0

我已經完成了你的建議,但仍然有同樣的例外 – developer

+0

你是否已經將所有的TCP動態端口改爲1433? – Parth

0

您需要配置遠程SQL服務器接受TCP/IP連接,如果未啓用,您可能需要允許遠程連接。