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

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

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

目 录CONTENT

文章目录

jdbc批量保存数据方法

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

批量保存数据代码

public int executeBatch(String sql, List<?> args) {
		if (null == args || 0 == args.size()) {
			throw new BaseException("参数为空");
		}
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql);
			int index = 0;
			for (int i = 0; i < args.size(); i++) {
				index++;
				Map<String, ?> maps = SysContext.objectToMap(args.get(i));
				int j = 1;
				List<String> lists = SQLHelper.getInsertRows(sql);
				for (String key : lists) {
					statement.setObject(j++, maps.get(StringUtil.convertHump(key)));
				}
				statement.addBatch();
				if (commit_count == index) {
					statement.executeBatch();
					statement.clearBatch();
					index = 0;
				}
			}
			if (0 != index) {
				statement.executeBatch();
				statement.clearBatch();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BaseException("批量执行保存SQL异常:" + e.getMessage());
		} finally {
			if (null != statement) {
				try {
					statement.close();
					statement = null;
				} catch (Exception e) {
				}
			}
		}
		return 1;
	}
0

评论区