问题出错

这里显示我们,要在后缀加上一个属性  这里默认设置为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

package com.xxx.wxjsxy;

import java.sql.*;

public class Jdbc {

	public static void main(String[] args) throws SQLException {
		Connection ct=null;
		Statement sm=null;
		ResultSet rs=null;
		int a=0;
		try {
			String url = "jdbc:sqlserver://localhost:1433;DatabaseName=bs_s1";
			// 连接数据库-forName:返回与带有给定字符串名的类或接口相关联的 Class 对象
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			// 得到连接-getConnection:建立到给定数据库 URL 的连接
			ct = DriverManager.getConnection(url,
					"sa", "123456");
			// 创建Statement-将 SQL 语句发送到数据库
			sm = ct.createStatement();
			rs = sm.executeQuery("select * from users");
			while(rs.next()){
				System.out.println(rs.getInt("userID")+","+rs.getString("userName")+","+rs.getString("passwd"));
			}
		} catch (ClassNotFoundException e) {
			// TODO: handle exception
			System.out.println("error loading driver:" + e);
		}
		if (rs!=null)
			rs.close();
		if (sm!=null) {
			sm.close();
		}
		if (ct!=null) {
			ct.close();
		}

	}

}

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

后缀加上;trustServerCertificate=true

这样就成功了

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐