(已解决)java连接数据库失败PKIX path building failed

环境:eclipse,sql server 2022,JDK19

用的JDBC:mssql-jdbc-12.2.0.jre11.jar

连接数据库时发生错误:

com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target”。

在网上找了一晚,没什么解决方案(我tm好难受啊)

在看了官方文档找到了答案;网址:使用加密进行连接 - JDBC Driver for SQL Server | Microsoft Learn

这是改之前的代码:

在url那加个属性,使encrypt 属性设置为 true 且 trustServerCertificate 属性设置为 true 时,Microsoft JDBC Driver for SQL Server 将不验证SQL Server TLS 证书

如图:

就这样解决了

要复制代码看官方文档,里面有的

使用加密进行连接 - JDBC Driver for SQL Server | Microsoft Learn

使用 JDBC 驱动程序 - JDBC Driver for SQL Server | Microsoft Learn