Jdbc批量数据操作

Jdbc批量数据操作

package jdbc;

import org.junit.Test;

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

/**
 * @author 小邱
 * @version 0.0.1
 * @description 批量数据操作
 * <p>
 * 当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条
 * 语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率
 * <p>
 * JDBC的批量处理语句包括下面三个方法:
 * addBatch(String):添加需要批量处理的SQL语句或是参数;
 * executeBatch():执行批量处理语句;
 * clearBatch():清空缓存的数据
 * 通常我们会遇到两种批量执行SQL语句的情况:
 * 多条SQL语句的批量处理;
 * 一个SQL语句的批量传参;
 * @since 2021/9/28 18:21
 */
public class PreparedStatementBatch {
    //实现层次一:使用Statement
    @Test
    public void test1() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        Statement statement = connection.createStatement();
        for (int i = 1; i <= 20000; i++) {
            String sql = "insert into goods(name) values('name_' + " + i + ")";
            statement.executeUpdate(sql);
        }
        JdbcUtils.close(connection, statement);
    }

    //实现层次二:使用PreparedStatement
    @Test
    public void test2() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        String sql = "insert into goods(name) values(?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //本质上还是一条一条执行(不算真正的批处理)
        for (int i = 0; i < 20000; i++) {
            preparedStatement.setObject(1, "name_" + i);
            preparedStatement.execute();
        }
        JdbcUtils.close(connection, preparedStatement);

    }

    // 实现层次三:使用 addBatch() / executeBatch() / clearBatch()
    //mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持。
    //?rewriteBatchedStatements=true 写在配置文件的url后面
    //mysql驱动5.1.37以上的新版本才支持
    @Test
    public void test3() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        String sql = "insert into goods(name) values(?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < 20000; i++) {
            preparedStatement.setObject(1, "name_" + i);
            //1.积攒sql
            preparedStatement.addBatch();
            if (i % 500 == 0) {//每500条执行一次
                //2.执行
                preparedStatement.executeBatch();
                //3.清空
                preparedStatement.clearBatch();
            }
        }
        JdbcUtils.close(connection, preparedStatement);
    }
    //实现层次四::在层次三的基础上操作 使用Connection 的 setAutoCommit(false) / commit()
    @Test
    public void test4() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        //设置为不自动提交数据
        connection.setAutoCommit(false);

        String sql = "insert into goods(name) values(?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < 20000; i++) {
            preparedStatement.setObject(1, "name_" + i);
            //1.积攒sql
            preparedStatement.addBatch();
            if (i % 1000 == 0) {//每500条执行一次
                //2.执行
                preparedStatement.executeBatch();
                //3.清空
                preparedStatement.clearBatch();
            }
        }
        //提交数据
        connection.commit();
        JdbcUtils.close(connection, preparedStatement);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值