java连接sqlserver驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。

290 阅读1分钟

报错信息

image.png

com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client preferences [TLS12]”。 ClientConnectionId:aa73676e-fa15-4c61-b11e-95a4a52e581a


Caused by: javax.net.ssl.SSLHandshakeException: The server selected protocol version TLS10 is not accepted by client preferences [TLS12]

解决办法

1、添加jtds依赖

<!--SqlServer驱动-->  
<dependency>  
    <groupId>com.microsoft.sqlserver</groupId>  
    <artifactId>mssql-jdbc</artifactId>  
    <version>7.4.1.jre8</version>  
</dependency>  
添加jtds依赖
<dependency>  
    <groupId>net.sourceforge.jtds</groupId>  
    <artifactId>jtds</artifactId>  
    <version>1.3.1</version>  
</dependency>

2、连接sql server 数据库

String url = "jdbc:jtds:sqlserver://localhost:1433/your_databasename";  
String user = "user";  
String password = "password";  
try {  
// 加载 JDTS 驱动程序  
Class.forName("net.sourceforge.jtds.jdbc.Driver");  
// 建立连接  
db_connection = DriverManager.getConnection(url, user, password);  
System.out.println("Connected to SQL Server successfully using JDTS.");  
} catch (ClassNotFoundException e) {  
    e.printStackTrace();  
System.out.println("JDTS Driver not found.");  
} catch (SQLException e) {  
    e.printStackTrace();  
    System.out.println("Connection failed.");  
}

这样就连接成功了