如何在 ubuntu 中通过 Java 连接 mssql?

如何在 ubuntu 中通过 Java 连接 mssql?

我正在尝试通过 ubuntu 中的 Java 连接我们的数据库。我正在使用 VPN 连接服务器。VPN 已成功连接,但当我运行程序时,我遇到了以下错误消息:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.1.35, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at Main.main(Main.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)**

我尝试过这些帖子,但没有效果: 来自 Ubuntu 的 MSSQL 连接 如何从 Ubuntu 14.04 连接 MSSQL 数据库?

这是我的Java代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {    
    Statement stmt = null;
    String query = "select * " +
                   "from DBName.dbo.tblCity" ;
    System.out.println(query);
    try {
        String URL = "jdbc:sqlserver://192.168.1.35:1433;databaseName=DBName;integratedSecurity=false";
        String USER = "USER";
        String PASS = "PASS.";

        Properties connectionProperties = new Properties();
        connectionProperties.put("username", USER);
        connectionProperties.put("password", PASS);
        Connection conn = DriverManager.getConnection(URL, connectionProperties);
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String strHomePageUrl=rs.getString("strCity");
               int intCityID=rs.getInt("intCityID");
               System.out.println(strHomePageUrl + " " + intCityID);
            System.out.println(strHomePageUrl + "\t" + intCityID );
        }
    } catch (SQLException e ) {
        System.out.println("Bağlantı kurulamadı");
        e.printStackTrace();
    } finally {
        if (stmt != null) {
            System.out.println("İşlem Başarılı");
            }
    }
}

}

它在 Windows 上也能运行。

您有什么办法可以解决这个问题吗?

相关内容