批处理Batch

JDBC的批量处理语句,由 PreparedStatement 处理,包括下面三个方法:
void addBatch():
将要执行的SQL先保存起来,先不执行
这个方法需要在在设置完所有的占位符之后调用

int[] executeBatch():执行批量处理语句;

void clearBatch(): 清空这个PreparedStatement对象当前的SQL命令列表

mysql默认批处理是关闭的,所以我们还需要去打开mysql的批处理:
我们需要在mysql的url地址中加入一下参数:
rewriteBatchedStatements=true
例如:url = jdbc:mysql://localhost:3306/jdbcstudy?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC

package com.jdbc.batch;

import com.JDBC_Utils.JDBCUtils;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Batch {
    @Test
    //传统加入数据方法
    public void noBatch() {
        Connection connection = null;
        String sql = "insert  into test values (?,?)";
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            //开始时间
            System.out.println("开始执行~");
            long start = System.currentTimeMillis();
            for (int i = 1; i <= 5000; i++) {
                preparedStatement.setObject(1, "Mike" + i);
                preparedStatement.setObject(2, i);
                preparedStatement.executeUpdate();
            }
            //结束时间
            long end = System.currentTimeMillis();
            System.out.println("传统方式耗费时间: " + (end - start)); //277702毫秒
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeConnection(null, preparedStatement, connection);
        }
    }

    @Test
    public void useBatch() {
        使用批处理加入数据
        Connection connection = null;
        String sql = "insert  into test values (?,?)";
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtils.getConnection();
            //取消连接的自动提交模式
            connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);
            //开始时间
            System.out.println("开始执行~");
            long start = System.currentTimeMillis();
            for (int i = 1; i <= 5000; i++) {
                preparedStatement.setObject(1, "Mike" + i);
                preparedStatement.setObject(2, i);
                preparedStatement.executeUpdate();
                //将sql语句 加入到 批处理包中
                preparedStatement.addBatch();
                //每有1000条数据, 就批量执行sql语句
                if (i % 1000 == 0) {
                    preparedStatement.executeBatch();
                    //装满1000之后就清空,准备装 下一波 的 1000个
                    preparedStatement.clearBatch();
                }
            }
            //结束时间
            long end = System.currentTimeMillis();
            System.out.println("批量方式耗费时间: " + (end - start));
            //提交事务  (不提交事务,则不会显示批量加入的数据)
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeConnection(null, preparedStatement, connection);
        }
    }
}

posted @   微风抚秀发  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示