侧边栏壁纸
博主头像
休闲猫

这个博主懒得的跟猪一样,别指望更新!!!

  • 累计撰写 14 篇文章
  • 累计创建 6 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

jdbc 查询直接返回对应对象

休闲猫
2022-08-03 / 0 评论 / 0 点赞 / 1314 阅读 / 0 字
温馨提示:
本文最后更新于2025-07-09,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

jdbc 查询直接返回对应对象

public <T> List<T> list(Class<T> classt, String sql, Object... args) {
		PreparedStatement statement = null;
		ResultSet rs = null;
		try {
			List<T> lists = new ArrayList<T>();
			statement = connection.prepareStatement(sql);
			if(null != args){
				for (int j = 0; j < args.length; j++) {
					statement.setObject(j+1, args[j]);
				}
			}			
			rs = statement.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();			
			while (rs.next()) {
				Map<String, Object> row = new HashMap<String, Object>();
				for (int i = 0; i < rsmd.getColumnCount(); i++) {
					String columLabel = rsmd.getColumnLabel(i+1);
					Object val = rs.getObject(columLabel);
					row.put(StringUtil.convertHump(columLabel), val);
				}
				lists.add(SysContext.mapToObject(row,classt));				
			}
			return lists;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BaseException("执行SQL异常:" + e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("SQL查询封装异常:" + e.getMessage());
		} finally {
			try {
				if (null != rs) {
					rs.close();
					rs = null;
				}
				if (null != statement) {
					statement.close();
					statement = null;
				}
			} catch (Exception e) {
			}
		}
	}
0

评论区