Java批量操作MySQL

高效地批量更新主机节点名称,通过分批次处理大量数据,减少单次更新的压力和提高效率。

int nationalTotalCount = updateNationalList.size();
if (CollectionUtils.isNotEmpty(updateNationalList)) {
    for (int startIndex = 0; startIndex < nationalTotalCount; startIndex += DEFAULT_PAGE_NUMBER) {
          int endIndex = Math.min(startIndex + DEFAULT_PAGE_NUMBER, nationalTotalCount);
          List<NationalVideoDevice> updateBatchList = updateNationalList.subList(startIndex, endIndex);
          log.info("批量更新主机节点名称 当前索引:{} 更新数量:{}", startIndex, updateBatchList.size());
          log.info("批量更新主机节点名称 开始时间:{}", System.currentTimeMillis());
          nationalVideoDeviceMapper.batchUpdate(updateBatchList);
          log.info("批量更新主机节点名称 结束时间:{}", System.currentTimeMillis());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java进行批量写入MySQL,可以使用JDBC批处理功能。以下是一个简单的示例代码,用于批量插入数据到MySQL表中: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class BatchInsertExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydatabase"; String username = "root"; String password = "password"; try { Connection conn = DriverManager.getConnection(url, username, password); String sql = "INSERT INTO mytable (column1, column2) VALUES (?, ?)"; // 插入语句 PreparedStatement statement = conn.prepareStatement(sql); // 开始批处理 conn.setAutoCommit(false); // 循环添加参数 for (int i = 0; i < 1000; i++) { statement.setString(1, "value" + i); statement.setString(2, "value" + (i+1)); statement.addBatch(); // 添加到批处理中 } // 执行批处理 int[] rowsInserted = statement.executeBatch(); // 提交事务 conn.commit(); System.out.println("成功插入 " + rowsInserted.length + " 行数据"); // 关闭连接和语句 statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 上述代码将使用批量处理将1000行数据插入名为`mytable`的MySQL表中。在循环中,通过`addBatch()`方法将每一行的参数添加到批处理中,最后使用`executeBatch()`方法执行批处理。在所有数据插入完成后,通过调用`commit()`方法提交事务。 请注意,上述代码中的URL、用户名和密码需要根据您的实际情况进行修改。还需要确保项目中已经添加了适当的MySQL驱动程序依赖。 希望对您有所帮助!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值