• 由于JDK17禁用了TLS 1.0,JDBC 驱动 12.10 默认只支持 TLS 1.2+,导致使用jdbc连接的时候出现错误:“com.microsoft.sqlserver.jdbc.SQLServerException: “Encrypt”属性设置为“true”且 “trustServerCertificate”属性设置为“false”,但驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server 建立安全连接:错误:The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]。”此时我把jdbc的url连接参数增加“encrypt=true;trustServerCertificate=true;”,依然异常:“com.microsoft.sqlserver.jdbc.SQLServerException: “Encrypt”属性设置为“true”且 “trustServerCertificate”属性设置为“true”,但驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server 建立安全连接:错误:The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]。”。经常查询资料遇到此中情况一般有两种解决方法,一个是修改jdk的$JAVA_HOME/conf/security/java.security文件,jdk.tls.disabledAlgorithms=SSLv3 # 移除 TLSv1 和 TLSv1.1;另外就是SQL Sever 2008打SP3补丁,可以在此连接搜索相应补丁 SQL Server 2008 Service Pack 3 ,搜索关键字"SQL Server 2008 Service Pack 3",以下是测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class JdbcSqlServerTest {
public static void main(String[] args) {

// 数据库连接信息
String url = "jdbc:sqlserver://localhost:1433;databaseName=db;encrypt=true;trustServerCertificate=true;";
String user = "sa";
String password = "000000";
Connection conn = null;
Statement stmt = null;
try {
// 加载驱动(JDK 6/7 必须写,JDK 8+ 可省略)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 建立连接
conn = DriverManager.getConnection(url, user, password);
System.out.println("连接成功!");
// 创建 Statement 并执行查询
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT TOP 10 * FROM user");

while (rs.next()) {
System.out.println("第一列值: " + rs.getString(1));
// 其他列处理略...
}

rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 测试jdbc驱动包,此驱动是目前最新的驱动包,数据库可以连接成功,但是会有警告:“警告: TLSv1 was negotiated. Please update server and client to use TLSv1.2 at minimum.”,如果想消除警告可以使用稍微老一点的驱动包。
1
2
3
4
5
6
7
	<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.10.0.jre11</version>
<scope>compile</scope>
</dependency>