[英]The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. How to solve this error?
[英]How to fix " The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption" error
我正在尝试通过 java 连接 MS SQL Server 2016。 我正在使用 JDK1.8 和 mssql-jdbc-7.2.1.jre8.jar 文件。
public class ConnectToMSSQL {
public static void main(String[] args) {
String connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=TestSQL;user=sa;password=password";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(connectionURL);
if (connection != null) {
System.out.println("Connected");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行上述代码时,出现以下错误,如何解决?
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "RSA premaster secret error". ClientConnectionId:8438be77-02cd-4d82-b84d-16c0045ac415
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2887)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1881)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2452)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2103)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1950)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1162)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:735)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.ConnectToMSSQL.main(ConnectToMSSQL.java:12)
Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
at sun.security.ssl.RSAClientKeyExchange.<init>(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1799)
... 8 more
Caused by: java.security.NoSuchAlgorithmException: SunTls12RsaPremasterSecret KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:169)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source)
... 18 more
它现在工作正常。 我刚刚完成了以下操作:在 Eclipse > Window > Preferences > Java > Installed JREs > 中删除并添加了相同的 JDK 路径,现在它工作正常。
你应该使用日志记录。 首先创建一个日志属性文件:
handlers= java.util.logging.ConsoleHandler, java.util.logging.FileHandler
.level= FINEST
java.util.logging.ConsoleHandler.level= FINEST
java.util.logging.FileHandler.level= FINEST
java.util.logging.FileHandler.pattern=%t/edpLog%u%g.log
java.util.logging.ConsoleHandler.formatter = java.util.logging.XmlFormatter
java.util.logging.FileHandler.formatter = java.util.logging.XmlFormatter
com.microsoft.sqlserver.jdbc.TDSChannel.level = FINEST
com.microsoft.sqlserver.jdbc.TDSReader.level = FINEST
com.microsoft.sqlserver.jdbc.TDSWriter.level = FINEST
com.microsoft.sqlserver.jdbc.TDS.DATA.level = FINEST
在%t
在%t/edpLog%u%g.log
意味着日志文件将被保存到你的系统临时文件夹。
接下来将日志记录类添加到您的类。
import java.util.logging.*;
public class ConnectToMSSQL {
private static Logger logger;
static {
System.setProperty("java.util.logging.config.file", "C:\\Path\\To\\Log\\Property\\File\\logging.properties");
logger = Logger.getLogger(ConnectToMSSQL.class.getName());
}
public static void main(String[] args) {
....
}
}
您应该获得一个日志文件并将输出发送到您的控制台。 这将是非常冗长,祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.