解决idea [08S01] 无法连接 sql server 数据库,报错:驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接

这篇具有很好参考价值的文章主要介绍了解决idea [08S01] 无法连接 sql server 数据库,报错:驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

因为sql server在jdbc连接的时候需要一定的安全验证,只需要在dbURL中把;trustServerCertificate=true加上后令其跳过就行了

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
 
public class Main {
    private static Connection dbConn = null;
 
    public static void main(String[] args) {
        String dbURL = "jdbc:sqlserver://localhost:1433;database=sjkjc;";//这里输入自己的数据库名称(即将Stu改为你自己的数据库名称)其余都可以不做修改
        try {
            //1.加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("加载驱动成功!");
            //2.连接
            dbConn = DriverManager.getConnection(dbURL, "sa", "123");//这里的密码改为第一步你所修改的密码,用户名一般就为"sa"
            System.out.println("连接数据库成功!");
            String sql="select Sname from Student";//这个语句就是表的查询语句,按照你所建的表修改名称
            PreparedStatement statement=null;
            statement=dbConn.prepareStatement(sql);
            ResultSet res=null;
            res=statement.executeQuery();
            while(res.next()){
                String title=res.getString("sname");
                System.out.println(title);
            }
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("连接数据库失败!");
        }
 
    }
}

上面的代码报错如下:

D:\JDK\jdk-11.0.18\bin\java.exe "-javaagent:D:\idea\IntelliJ IDEA 2023.1\lib\idea_rt.jar=54456:D:\idea\IntelliJ IDEA 2023.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\张艺达\IdeaProjects\untitled\out\production\untitled;D:\JDBC\sqljdbc_12.2\chs\mssql-jdbc-12.2.0.jre11.jar Main
加载驱动成功!
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”。 ClientConnectionId:90f72b77-2a96-4ca6-b51f-995cd5499215
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:3937)
	at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1950)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:3456)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:3077)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2919)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1787)
	at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1229)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
	at Main.main(Main.java:15)
Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:369)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:312)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:307)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:654)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.onCertificate(CertificateMessage.java:473)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.consume(CertificateMessage.java:369)
	at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:392)
	at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:478)
	at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:456)
	at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:199)
	at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:172)
	at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1383)
	at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1296)
	at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:416)
	at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:388)
	at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1839)
	... 8 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:439)
	at java.base/sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:306)
	at java.base/sun.security.validator.Validator.validate(Validator.java:264)
	at java.base/sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:313)
	at java.base/sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:233)
	at java.base/sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:110)
	at com.microsoft.sqlserver.jdbc.HostNameOverrideX509TrustManager.checkServerTrusted(SQLServerTrustManager.java:86)
	at java.base/sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(SSLContextImpl.java:1510)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:638)
	... 20 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at java.base/sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
	at java.base/sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
	at java.base/java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)
	at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:434)
	... 28 more
连接数据库失败!
 
进程已结束,退出代码0

在dbURL中把;trustServerCertificate=true加上后就没有报错了

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
 
public class Main {
    private static Connection dbConn = null;
 
    public static void main(String[] args) {
        String dbURL = "jdbc:sqlserver://localhost:1433;database=sjkjc;trustServerCertificate=true";//这里输入自己的数据库名称(即将Stu改为你自己的数据库名称)其余都可以不做修改
        try {
            //1.加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("加载驱动成功!");
            //2.连接
            dbConn = DriverManager.getConnection(dbURL, "sa", "123");//这里的密码改为第一步你所修改的密码,用户名一般就为"sa"
            System.out.println("连接数据库成功!");
            String sql="select Sname from Student";//这个语句就是表的查询语句,按照你所建的表修改名称
            PreparedStatement statement=null;
            statement=dbConn.prepareStatement(sql);
            ResultSet res=null;
            res=statement.executeQuery();
            while(res.next()){
                String title=res.getString("sname");
                System.out.println(title);
            }
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("连接数据库失败!");
        }
 
    }
}

无报错

因为sql server在jdbc连接的时候需要一定的安全验证,
只需要在dbURL中把;trustServerCertificate=true加上后令其跳过就行了文章来源地址https://www.toymoban.com/news/detail-842784.html

到了这里,关于解决idea [08S01] 无法连接 sql server 数据库,报错:驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包