29.jdbc实现通用的方法--更新(增删改)、查询例子,通用--实体类的属性名和数据库的列名必须一致

本文介绍如何使用Java JDBC编写通用的方法来执行数据库的更新和查询操作,强调实体类属性与数据库列名的一致性要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class BaseDao {
	
	// 初始化参数
	private Connection con;
	private PreparedStatement pstmt;
	private ResultSet rs;

	/**
	 * 更新的通用方法
	 * @param sql   更新的sql语句(update/insert/delete)
	 * @param paramsValue  sql语句中占位符对应的值(如果没有占位符,传入null)
	 */
	public void update(String sql,Object[] paramsValue){
		
		try {
			// 获取连接
			con = JdbcUtil.getConnection();
			// 创建执行命令的stmt对象
			pstmt = con.prepareStatement(sql);
			// 参数元数据: 得到占位符参数的个数
			int count = pstmt.getParameterMetaData().getParameterCount();
			
			// 设置占位符参数的值
			if (paramsValue != null && paramsValue.length > 0) {
				// 循环给参数赋值
				for(int i=0;i<count;i++) {
					pstmt.setObject(i+1, paramsValue[i]);
				}
			}
			// 执行更新
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.closeAll(con, pstmt, null);
		}
	}
	
	/**
	 * 查询的通用方法
	 * @param sql
	 * @param paramsValue
	 */
	public <T> List<T> query(String sql, Object[] paramsValue,Class<T> clazz){
		
		try {
			// 返回的集合
			List<T> list = new ArrayList<T>();
			// 对象
			T t = null;
			
			// 1. 获取连接
			con = JdbcUtil.getConnection();
			// 2. 创建stmt对象
			pstmt = con.prepareStatement(sql);
			// 3. 获取占位符参数的个数, 并设置每个参数的值
			//int count = pstmt.getParameterMetaData().getParameterCount();
			if (paramsValue != null && paramsValue.length > 0) {
				for (int i=0; i<paramsValue.length; i++) {
					pstmt.setObject(i+1, paramsValue[i]);
				}
			}
			// 4. 执行查询
			rs = pstmt.executeQuery();
			// 5. 获取结果集元数据
			ResultSetMetaData rsmd = rs.getMetaData();
			// ---> 获取列的个数
			int columnCount = rsmd.getColumnCount();
			
			// 6. 遍历rs
			while (rs.next()) {
				// 要封装的对象
				t = clazz.newInstance();
				
				// 7. 遍历每一行的每一列, 封装数据
				for (int i=0; i<columnCount; i++) {
					// 获取每一列的列名称
					String columnName = rsmd.getColumnName(i + 1);
					// 获取每一列的列名称, 对应的值
					Object value = rs.getObject(columnName);
					// 封装: 设置到t对象的属性中  【BeanUtils组件】
					BeanUtils.copyProperty(t, columnName, value);				
				}
				
				// 把封装完毕的对象,添加到list集合中
				list.add(t);
			}
			
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.closeAll(con, pstmt, rs);
		}
	}
}

用到了另一个类

public class JdbcUtil {

	// 连接参数
	// private String url = "jdbc:mysql://localhost:3306/jdbc_demo";
	private static String url = "jdbc:mysql:///jdbc_demo";
	private static String user = "root";
	private static String password = "root";

	/**
	 * 返回连接对象
	 */
	public static Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 关闭
	 */
	public static void closeAll(Connection con, Statement stmt, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();  // 快速异常捕获 Alt + shift + z 
				rs = null;   // 建议垃圾回收期回收资源
			}
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
			if (con != null && !con.isClosed()) {
				con.close();
				con = null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值