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("连接数据库失败!");
}
}
}
无报错文章来源:https://www.toymoban.com/news/detail-516890.html
因为sql server在jdbc连接的时候需要一定的安全验证,只需要在dbURL中把;trustServerCertificate=true加上后令其跳过就行了文章来源地址https://www.toymoban.com/news/detail-516890.html
到了这里,关于idea 中无法连接 sql server 数据库,报错:驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!