zoukankan      html  css  js  c++  java
  • MYSQL 之 JDBC(十四):批量处理JDBC语句提高处理效率

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

    2.JDBC的批量处理语句包括下面两个方法:

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

    通常我们会遇到两种批量执行SQL语句的情况:

    • 多条SQL语句的批量处理
    • 一个SQL语句的批量传参

    代码(3种批量处理方式对比):

    复制代码

    package com.litian.jdbc;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    
    /**
     * @author: Li Tian
     * @contact: litian_cup@163.com
     * @software: IntelliJ IDEA
     * @file: JDBCTest4.java
     * @time: 2020/4/4 14:18
     * @desc: |批量处理事务
     */
    
    public class JDBCTest4 {
    
        public static void main(String[] args){
            // testStatement();
            testPrepareStatement();
        }
    
        /**
         * 向数据库的数据表中插入10w条记录
         * 测试如何插入,用时最短
         * 1. 使用Statement:15038
         */
        public static void testStatement() {
            Connection conn = null;
            Statement st = null;
            String sql = null;
            try {
                conn = JDBCTools.getConnection();
                JDBCTools.beginTx(conn);
                st = conn.createStatement();
    
                long begin = System.currentTimeMillis();
                for (int i = 0; i < 100000; i++) {
                    sql = String.format("insert into t_user2(username, pwd) values(name_%d, 6666)", i);
                    st.executeUpdate(sql);
                }
                long end = System.currentTimeMillis();
                System.out.println(end - begin);
    
                JDBCTools.commit(conn);
            } catch (Exception e) {
                e.printStackTrace();
                JDBCTools.rollback(conn);
            } finally {
                JDBCTools.release(null, st, conn);
            }
        }
    
        /**
         * 向数据库的数据表中插入10w条记录
         * 测试如何插入,用时最短
         * 2. 使用PreparedStatement:13131
         * 3. 在2的基础上使用批量处理:24596?这就很尴尬了
         */
        public static void testPrepareStatement() {
            Connection conn = null;
            PreparedStatement st = null;
            String sql = null;
            try {
                conn = JDBCTools.getConnection();
                JDBCTools.beginTx(conn);
                sql = "insert into t_user2(username, pwd) values(?,?)";
                st = conn.prepareStatement(sql);
    
                long begin = System.currentTimeMillis();
                for (int i = 0; i < 100000; i++) {
                    st.setString(1, "name_" + i);
                    st.setString(2, "6666");
                    st.executeUpdate();
    
                    // “积攒”sql语句
                    st.addBatch();;
                    // 当积攒到一定程度,就统一地执行一次,并且清空先前积攒的sql
                    if((i + 1) % 300 == 0){
                        st.executeBatch();
                        st.clearBatch();
                    }
                }
    
                // 若总条数不是批量数值的整数倍,则还需要额外再执行一次
                if(100000 % 300 != 0){
                    st.executeBatch();
                    st.clearBatch();
                }
    
                long end = System.currentTimeMillis();
                System.out.println(end - begin);
    
                JDBCTools.commit(conn);
            } catch (Exception e) {
                e.printStackTrace();
                JDBCTools.rollback(conn);
            } finally {
                JDBCTools.release(null, st, conn);
            }
        }
    }
  • 相关阅读:
    输入/输出的格式和方法
    程序编译运行和exe运行之文件位置的区别
    ZOJ_3950_How Many Nines 解题报告及如何对程序进行测试修改
    [Offer收割]编程练习赛13 解题报告
    查找语言自带函数
    codeblocks下的汇编语言
    hiho一下 第144周(机会渺茫)解题报告及拓展
    关闭调试窗口快捷方式
    编写程序一个位置的快速到达和修改
    poj3660(Cow Contest)解题报告
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13308989.html
Copyright © 2011-2022 走看看