JDBC 批处理

在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率。 
JDBC实现批处理有两种方式:

  • 第一种方式:Statement.addBatch(sql)
  • 第二种方式:PreparedStatement.addBatch()

使用Statement完成批处理

  1. 使用Statement对象添加要批量执行的SQL语句,如下:

    Statement.addBatch(sql1);
    Statement.addBatch(sql2);
    Statement.addBatch(sql3);
    • 1
    • 2
    • 3
  2. 执行批处理SQL语句

    Statement.executeBatch();
    • 1
  3. 清除批处理命令

    Statement.clearBatch();
    • 1

使用Statement完成批处理范例

  1. 编写测试的SQL脚本创建表。

    create table testbatch
    (
        id varchar(40) primary key,
        name varchar(40)
    );
    • 1
    • 2
    • 3
    • 4
    • 5
  2. 编写测试代码,如下所示:

    public class Demo3 {
    
        /*
        create table testbatch
        (
            id varchar(40) primary key,
            name varchar(40)
        );
    
        */
    
        // 实现批处理的第一种方式
        @Test
        public void test1() throws SQLException {
    
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
    
            try {
                conn = JdbcUtils.getConnection();
                String sql1 = "insert into testbatch(id,name) values('1','李阿云')"; // 1.可以发送不同的sql语句 2.发送的sql语句没有预编译
                String sql2 = "update testbatch set name='李阿昀' where id='1'";
    
                st = conn.createStatement(); // Statement内部维护了一个List集合,addBatch()方法加入的sql语句都加入到该List集合内了。
                st.addBatch(sql1);
                st.addBatch(sql2);
    
                st.executeBatch(); // 返回int[]  [3,4]——即第一条sql语句执行影响数据库表几行,第二条sql语句执行影响数据库表几行,...
                st.clearBatch(); // 清除清除批处理命令
            } finally {
                JdbcUtils.release(conn, st, rs);
            }
    
        }
    
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

采用Statement.addBatch(sql)方式实现批处理的优缺点

采用Statement.addBatch(sql)方式实现批处理:

  • 优点:可以向数据库发送多条不同的SQL语句。
  • 缺点:

    • SQL语句没有预编译。
    • 当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条SQL语句。例如:

      Insert into user(name,password) values(‘aa’,’111’);
      Insert into user(name,password) values(‘bb’,’222’);
      Insert into user(name,password) values(‘cc’,’333’);
      Insert into user(name,password) values(‘dd’,’444’);
      • 1
      • 2
      • 3
      • 4

使用PreparedStatement完成批处理

使用PreparedStatement完成批处理范例

public class Demo3 {

    /*
    create table testbatch
    (
        id varchar(40) primary key,
        name varchar(40)
    );

    */
    // 实现批处理的第二种方式
    @Test
    public void test2() throws SQLException {

        long starttime = System.currentTimeMillis();
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtils.getConnection();
            String sql1 = "insert into testbatch(id,name) values(?,?)"; // 1.适合做批量插入和批量更新   2.发送的sql语句都预编译过,性能会好一点。实际开发里,此种方式用到的多一点。
            st = conn.prepareStatement(sql1);

            /*
            st.setString(1, "1");
            st.setString(2, "李阿云");
            st.addBatch(); // 把PreparedStatement对象内部封装的sql语句加入到PreparedStatement对象内部的List集合中

            st.setString(1, "2");
            st.setString(2, "叶一");
            st.addBatch();  // 此时PreparedStatement对象内部的List集合中有两条sql语句
            */

            // 批处理一千万条sql语句,会出现OutOfMemoryError:Java heap space——内存溢出
            /*
            for (int i = 1; i <= 10000000; i++) {
                st.setString(1, i+"");
                st.setString(2, "叶一"+i);
                st.addBatch();
            }
            st.executeBatch();
            */
            for (int i = 1; i <= 10000006; i++) {
                st.setString(1, i+"");
                st.setString(2, "叶一"+i);
                st.addBatch();

                if (i%1000==0) {
                    st.executeBatch();
                    st.clearBatch();
                }
                st.executeBatch(); // 剩下不到1000条sql语句也需要执行
            }
        } finally {
            JdbcUtils.release(conn, st, rs);
        }

        long endtime = System.currentTimeMillis();
        System.out.println("共花了:"+(endtime-starttime)/1000+"秒"); // 向MySQL数据库批插入1000万条记录,大概需要3个多小时。而Oracle数据库测试大概需要380秒
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

采用PreparedStatement.addBatch()方式实现批处理的优缺点

采用PreparedStatement.addBatch()实现批处理

  • 优点:发送的是预编译后的SQL语句,执行效率高。
  • 缺点:只能应用在SQL语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据。

关于JDBC批处理的内容就总结这么多。

转自:https://blog.csdn.net/yerenyuan_pku/article/details/52304317

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值