使用 Java (netbeans) 连接到远程 SQL 服务器时出现 SSL 错误

问题描述 投票:0回答:1

我之前问过这个问题,但没有得到答案,我想主要是因为它很混乱,我会陈述事实并在下面引用一些代码

  • 我使用 VMWare,并且有一台 Windows Server 2003,上面装有 SQL Server 2005,具有 SQL 登录名,并且正在运行 DNS 服务
  • 我还有一台运行 NetBeans8.2 和 JDK 8.1 的 Windows 7 计算机,使用 JDBC 4.2
  • 我可以使用 SQL Manager 从 Windows 7 计算机连接到服务器
  • 由于 SSL 错误,我无法使用 java 代码进行连接,我不确定是什么原因导致的
  • 这是一个学校项目,所以我必须使用 SQL Server 2005

这是我的连接代码:

    package connectbd;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    
    public class ConnectBD {

    public static void main(String[] args) {

        String jdbcurl;
        Connection con = null;

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        jdbcurl = "jdbc:sqlserver://SQLSERVER;instanceName=SQLE;user=****;password=****;database=LP_SIBD_GR15";
        
        try {
            con=DriverManager.getConnection(jdbcurl);
            System.out.println("Connection success");
        } catch(SQLException e) {
                e.printStackTrace();
        }
    }
    }

这是我得到的错误:

com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to the SQL Server using Secure Sockets Layer (SSL) encryption. Error: "The SQL Server server returned no response. The connection has been closed. ClientConnectionId:e9655c34-7c66-42c8-aaec-36601b53ff98 ».
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2826)
    ****at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1829)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2391)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2042)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1889)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1120)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:700)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at connectbd.ConnectBD.main(ConnectBD.java:35)
Caused by: java.io.IOException: 
The SQL Server server returned no response. The connection was closed. ClientConnectionId:e9655c34-7c66-42c8-aaec-36601b53ff98
    ****at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.ensureSSLPayload(IOBuffer.java:786)
    at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.readInternal(IOBuffer.java:836)
    at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.read(IOBuffer.java:829)
    at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.readInternal(IOBuffer.java:999)
    at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.read(IOBuffer.java:989)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1756)
    ... 8 more
BUILD SUCCESSFUL (total time: 1 second)

我相信原因就在这两行中:开头标有 **** 的行

我必须在两周内为学校交付一个网络服务项目,这是一个巨大的障碍,任何快速的帮助或建议将非常感激

java sql-server ssl netbeans sql-server-2005
1个回答
4
投票

您需要添加一些参数来指定 ssl 连接,例如

integratedSecurity=true
encrypt=true
trustServerCertificate=true

jdbcurl = "jdbc:sqlserver://SQLSERVER;instanceName=SQLE;user=****;password=****;database=LP_SIBD_GR15;integratedSecurity=true;encrypt=true;trustServerCertificate=true";
© www.soinside.com 2019 - 2024. All rights reserved.