当前位置:   article > 正文

Exception in thread “main“ com.microsoft.sqlserver.jdbc.SQLServerException: “Encrypt”属性设置为“true”且 “t_exception in thread "main" com.microsoft.sqlserver

exception in thread "main" com.microsoft.sqlserver.jdbc.sqlserverexception:

 问题出错

这里显示我们,要在后缀加上一个属性  这里默认设置为false

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: “Encrypt”属性设置为“true”且 “trustServerCertificate”属性设置为“false”,但驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server 建立安全连接:错误:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target。 ClientConnectionId:f8eb2dc2-65b1-4ea6-826a-d3350235e243

  1. package com.xxx.wxjsxy;
  2. import java.sql.*;
  3. public class Jdbc {
  4. public static void main(String[] args) throws SQLException {
  5. Connection ct=null;
  6. Statement sm=null;
  7. ResultSet rs=null;
  8. int a=0;
  9. try {
  10. String url = "jdbc:sqlserver://localhost:1433;DatabaseName=bs_s1";
  11. // 连接数据库-forName:返回与带有给定字符串名的类或接口相关联的 Class 对象
  12. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  13. // 得到连接-getConnection:建立到给定数据库 URL 的连接
  14. ct = DriverManager.getConnection(url,
  15. "sa", "123456");
  16. // 创建Statement-将 SQL 语句发送到数据库
  17. sm = ct.createStatement();
  18. rs = sm.executeQuery("select * from users");
  19. while(rs.next()){
  20. System.out.println(rs.getInt("userID")+","+rs.getString("userName")+","+rs.getString("passwd"));
  21. }
  22. } catch (ClassNotFoundException e) {
  23. // TODO: handle exception
  24. System.out.println("error loading driver:" + e);
  25. }
  26. if (rs!=null)
  27. rs.close();
  28. if (sm!=null) {
  29. sm.close();
  30. }
  31. if (ct!=null) {
  32. ct.close();
  33. }
  34. }
  35. }

 这样写就会出现问题,因为默认是false

后缀加上;trustServerCertificate=true

这样就成功了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/753999
推荐阅读
相关标签
  

闽ICP备14008679号