JDBC p4 批处理

打印 上一主题 下一主题

主题 902|帖子 902|积分 2706

批处理


  • 基本介绍:

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

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

    • JDBC连接MySQL时,如果要使用批处理功能,请在url中加入参数:rewriteBatchedStatements=true
    • 批处理往往和PreparedStatement一起搭配使用,可以即减少编译次数,又减少运行次数,效率大大提高。
    • 批处理是将我们的SQL语句先存放在ArrayList中,当添加到指定的值后就executeBatch,批量处理。批处理会减少我们发送sql语句的网络开销,而且减少编译次数,因此效率提高。
    案例:
    1. package com.hspedu.jdbc.batch_;
    2. import com.hspedu.jdbc.utils.JDBCUtils;
    3. import org.junit.jupiter.api.Test;
    4. import java.sql.Connection;
    5. import java.sql.PreparedStatement;
    6. import java.sql.SQLException;
    7. import java.sql.Statement;
    8. /**
    9. * @author: 86199
    10. * @date: 2023/7/20 15:34
    11. * Description: 演示Java 的 批处理
    12. */
    13. public class Batch_ {
    14.     //传统方法添加数据到admin2表
    15.     @Test
    16.     public void noBatch(){
    17.         //得到连接
    18.         Connection connection;
    19.         PreparedStatement preparedStatement;
    20.         try {
    21.             connection = JDBCUtils.getConnection();
    22.             String sql = "insert into admin2 values (NULL, ?, ?)";
    23.             preparedStatement = connection.prepareStatement(sql);
    24.             System.out.println("开始执行");
    25.             long start = System.currentTimeMillis();
    26.             for (int i = 0; i < 5000; i++) {
    27.                 preparedStatement.setString(1, "jack" + i);
    28.                 preparedStatement.setString(2, "666");
    29.                 //执行
    30.                 preparedStatement.executeUpdate();
    31.             }
    32.             long end = System.currentTimeMillis();
    33.             System.out.println("传统方式 耗时 = " + (end - start));//传统方式 耗时 = 4825
    34.         } catch (SQLException e) {
    35.             throw new RuntimeException(e);
    36.         }
    37.         //关闭资源
    38.         JDBCUtils.close(null, preparedStatement, connection);
    39.     }
    40.     //使用批量方式添加数据
    41.     @Test
    42.     public void batch(){
    43.         //得到连接
    44.         Connection connection;
    45.         PreparedStatement preparedStatement;
    46.         try {
    47.             connection = JDBCUtils.getConnection();
    48.             String sql = "insert into admin2 values (NULL, ?, ?)";
    49.             preparedStatement = connection.prepareStatement(sql);
    50.             System.out.println("开始执行");
    51.             long start = System.currentTimeMillis();
    52.             for (int i = 0; i < 5000; i++) {
    53.                 preparedStatement.setString(1, "jack" + i);
    54.                 preparedStatement.setString(2, "666");
    55.                 //将 sql语句加入批处理包
    56.                 //执行
    57.                 preparedStatement.addBatch();
    58.                 //当有5000条时,再批量执行
    59.                 if((i + 1) % 1000 == 0){
    60.                     preparedStatement.executeBatch();
    61.                     //清空
    62.                     preparedStatement.clearBatch();
    63.                 }
    64.             }
    65.             long end = System.currentTimeMillis();
    66.             System.out.println("批量包方式 耗时 = " + (end - start));//批处理方式 耗时 = 87
    67.         } catch (SQLException e) {
    68.             throw new RuntimeException(e);
    69.         }
    70.         //关闭资源
    71.         JDBCUtils.close(null, preparedStatement, connection);
    72.     }
    73. }
    复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美食家大橙子

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表