美食家大橙子 发表于 2023-7-28 16:23:16

JDBC p4 批处理

批处理


[*]基本介绍:

[*]当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率。
[*]JDBC的批量处理语句包括下面方法:

[*]addBatch():添加需要批量处理的SQL语句或参数;
[*]executeBatch():执行批量处理语句;
[*]clearBatch():清空批处理包的语句;

[*]JDBC连接MySQL时,如果要使用批处理功能,请在url中加入参数:rewriteBatchedStatements=true。
[*]批处理往往和PreparedStatement一起搭配使用,可以即减少编译次数,又减少运行次数,效率大大提高。
[*]批处理是将我们的SQL语句先存放在ArrayList中,当添加到指定的值后就executeBatch,批量处理。批处理会减少我们发送sql语句的网络开销,而且减少编译次数,因此效率提高。
案例:
package com.hspedu.jdbc.batch_;

import com.hspedu.jdbc.utils.JDBCUtils;
import org.junit.jupiter.api.Test;

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

/**
* @author: 86199
* @date: 2023/7/20 15:34
* Description: 演示Java 的 批处理
*/
public class Batch_ {
    //传统方法添加数据到admin2表
    @Test
    public void noBatch(){
      //得到连接
      Connection connection;
      PreparedStatement preparedStatement;

      try {
            connection = JDBCUtils.getConnection();

            String sql = "insert into admin2 values (NULL, ?, ?)";
            preparedStatement = connection.prepareStatement(sql);


            System.out.println("开始执行");
            long start = System.currentTimeMillis();
            for (int i = 0; i < 5000; i++) {
                preparedStatement.setString(1, "jack" + i);
                preparedStatement.setString(2, "666");
                //执行
                preparedStatement.executeUpdate();
            }
            long end = System.currentTimeMillis();
            System.out.println("传统方式 耗时 = " + (end - start));//传统方式 耗时 = 4825
      } catch (SQLException e) {
            throw new RuntimeException(e);
      }

      //关闭资源
      JDBCUtils.close(null, preparedStatement, connection);
    }


    //使用批量方式添加数据
    @Test
    public void batch(){
      //得到连接
      Connection connection;
      PreparedStatement preparedStatement;

      try {
            connection = JDBCUtils.getConnection();

            String sql = "insert into admin2 values (NULL, ?, ?)";
            preparedStatement = connection.prepareStatement(sql);


            System.out.println("开始执行");
            long start = System.currentTimeMillis();
            for (int i = 0; i < 5000; i++) {
                preparedStatement.setString(1, "jack" + i);
                preparedStatement.setString(2, "666");
                //将 sql语句加入批处理包
                //执行
                preparedStatement.addBatch();
                //当有5000条时,再批量执行
                if((i + 1) % 1000 == 0){
                  preparedStatement.executeBatch();
                  //清空
                  preparedStatement.clearBatch();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("批量包方式 耗时 = " + (end - start));//批处理方式 耗时 = 87
      } catch (SQLException e) {
            throw new RuntimeException(e);
      }

      //关闭资源
      JDBCUtils.close(null, preparedStatement, connection);
    }
}

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: JDBC p4 批处理