zoukankan      html  css  js  c++  java
  • JDBC进行批处理Batch

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

      JDBC实现批处理有两种方式:statement和preparedstatement

    一、使用Statement完成批处理

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

    CREATE TABLE `testbatch` (
      `id` int(11) NOT NULL,
      `name` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

      2.测试代码

    public class jdbcBatch {
        @Test
        public void testJdbcBatchHandleByStatement(){
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            try{
                conn = JdbcUtils.getConnection();
                String sql1 = "insert into testbatch(id,name) values(1,'aaa')";
                String sql2 = "insert into testbatch(id,name) values(2,'bbb')";
                String sql3 = "insert into testbatch(id,name) values(3,'CCC')";
                String sql4 = "insert into testbatch(id,name) values(4,'DDD')";
                String sql5 = "update testbatch set name='gacl' where id=1";
                String sql6 = "insert into testbatch(id,name) values(5,'FFF')";
                String sql7 = "delete from testbatch where id=2";
                st = conn.createStatement();
                //添加要批量执行的SQL
                st.addBatch(sql1);
                st.addBatch(sql2);
                st.addBatch(sql3);
                st.addBatch(sql4);
                st.addBatch(sql5);
                st.addBatch(sql6);
                st.addBatch(sql7);
                //执行批处理SQL语句
                st.executeBatch();
                //清除批处理命令
                st.clearBatch();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }
    }

      3.采用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');

    二、使用PreparedStatement完成批处理

      1.使用PreparedStatement完成批处理范例,测试代码

    public class JdbcBatchPrepareStatement {
        @Test
        public void testJdbcBatchHandleByPrepareStatement(){
            long starttime = System.currentTimeMillis();
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
    
            try{
                conn = JdbcUtils.getConnection();
                String sql = "insert into testbatch(id,name) values(?,?)";
                st = conn.prepareStatement(sql);
                for(int i=6;i<10;i++){  //i=1000  2000
                    st.setInt(1, i);
                    st.setString(2, "aa" + i);
                    st.addBatch();
                }
                st.executeBatch();
                st.clearBatch();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
            long endtime = System.currentTimeMillis();
            System.out.println("程序花费时间:" + (endtime-starttime)/1000 + "秒!!");
        }
    }

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

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

  • 相关阅读:
    【leetcode】1295. Find Numbers with Even Number of Digits
    【leetcode】427. Construct Quad Tree
    【leetcode】1240. Tiling a Rectangle with the Fewest Squares
    【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
    【leetcode】1291. Sequential Digits
    【leetcode】1290. Convert Binary Number in a Linked List to Integer
    【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
    【leetcode】1289. Minimum Falling Path Sum II
    【leetcode】1288. Remove Covered Intervals
    【leetcode】1287. Element Appearing More Than 25% In Sorted Array
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/9524695.html
Copyright © 2011-2022 走看看