1

我正在尝试从系统访问数据库到其他系统,下面是代码。

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(); 
        } 

    }

}

下面是我得到的异常。

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)

我能够从同一个系统连接到数据库。但现在我正在尝试以远程方式连接。

我在 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
4

2 回答 2

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

它完成了......

于 2012-11-08T07:02:34.247 回答
0

you need to configure the remote SQL server to accept TCP/IP connection and you may require to allow remote connection in case if it not enabled.

于 2012-11-08T11:18:59.393 回答