设为首页 加入收藏

TOP

JDBC p4 批处理
2023-08-06 07:49:58 】 浏览:63
Tags:JDBC

批处理

  • 基本介绍:

    1. 当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率。
    2. JDBC的批量处理语句包括下面方法:
      • addBatch():添加需要批量处理的SQL语句或参数;
      • executeBatch():执行批量处理语句;
      • clearBatch():清空批处理包的语句;
    3. JDBC连接MySQL时,如果要使用批处理功能,请在url中加入参数:rewriteBatchedStatements=true
    4. 批处理往往和PreparedStatement一起搭配使用,可以即减少编译次数,又减少运行次数,效率大大提高。
    5. 批处理是将我们的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);
        }
    }
    
    
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇策略模式+Spring配置类优化多if.... 下一篇体验IntelliJ IDEA的远程开发(Re..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目