使用java(netbeans)连接到远程sql server时出现ssl错误

9rygscc1  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(452)

我以前问过这个问题,但没有得到答案,我想主要是因为这是一个混乱,我会陈述事实,并把一些引用代码下
我使用的是vmware,有一个windowsserver2003,上面有sqlserver2005,有一个sql登录名,还有一个dns服务正在运行
我还有一台运行netbeans8.2和jdk8.1的windows7机器,使用jdbc4.2
我可以使用Windows7机器上的sql管理器连接到服务器
由于ssl错误,我无法使用java代码进行连接,我不确定是什么原因造成的
这是一个学校的项目,所以我必须使用SQLServer2005
这是我的连接代码:

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)

我相信原因就在这两条线上:一开始用****标记的
我必须在2周内为学校提供一个webservices项目,这是一个巨大的障碍,任何快速的帮助或建议将不胜感激

c8ib6hqw

c8ib6hqw1#

您需要添加一些参数来指定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";

相关问题