JDBC(10)—批处理

来源:互联网 发布:淘宝捉猫猫是干嘛的 编辑:程序博客网 时间:2024/06/03 22:41
  1. 批量处理JDBC语句,提高处理速度。
  2. 当需要成批的的插入或更新记录时可以采用java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理,通常情况下比单独提交处理更有效率。
  3. 批量处理的方法:
    • ——addBatch(String):添加需要批量处理的SQL语句或是参数。
    • ——executeBatch():执行批量处理语句。
      *通常我们会遇到两种批量执行SQL语句的情况:
    • ——多条SQL语句批量处理
    • ——一条SQL语句的批量传参
  4. 以下三个方法使用了三种批量处理方式以及使用时间分别为:
    *Statement使用时间:18271毫秒
    *PreparedStatement使用时间:13808毫秒
    *JDBC批处理:2046毫秒
  5. 实例
public class Volume_11 {    /**     * 1.向Sql Server中表customers插入100000条记录。     *  使用statement,花费时间:18271毫秒     */    @Test    public void testBatchWithStatement(){        Connection conn = null;        Statement statement = null;        String sql = null;        try {            conn = TestTools.getConnection();            //事务开始            TestTools.beginTx(conn);            statement = conn.createStatement();            //开始计时            long begin = System.currentTimeMillis();            for(int i = 0; i < 100000; i++){                sql = "INSERT INTO customers VALUES('"+                                (i+1)+"','name_"+i+"','29-6月-13')";                statement.executeUpdate(sql);            }            //计时结束            long end = System.currentTimeMillis();            System.out.println("时间:"+(end - begin));            //事务提交            TestTools.commit(conn);        } catch (Exception e) {            e.printStackTrace();            //事务回滚            TestTools.rollback(conn);        }finally{            TestTools.release(statement, conn);        }    }    /**     * 2.向Sql Server中表customers插入100000条记录。     * 使用Preparedstatement,花费时间:13808毫秒     */    @Test    public void testBatchWithPreparedstatament(){        Connection conn = null;        PreparedStatement preparedstatement = null;        String sql = null;        try {            conn = TestTools.getConnection();            //事务开始            TestTools.beginTx(conn);            sql = "INSERT INTO customers VALUES(?,?,?)";            preparedstatement = conn.prepareStatement(sql);            Date date = new Date(new java.util.Date().getTime());            //开始计时            long begin = System.currentTimeMillis();            for(int i = 0; i < 100000; i++){                preparedstatement.setInt(1, (i+1));                preparedstatement.setString(2, "name_"+i);                preparedstatement.setDate(3, date);                preparedstatement.executeUpdate();            }            //计时结束            long end = System.currentTimeMillis();            System.out.println("时间:"+(end - begin));            //事务提交            TestTools.commit(conn);        } catch (Exception e) {            e.printStackTrace();            //事务回滚            TestTools.rollback(conn);        }finally{            TestTools.release(preparedstatement, conn);        }    }    /**     * 3.向Sql Server中表customers插入100000条记录。     * 使用JDBC批处理,花费时间:2046毫秒     */    @Test    public void testBatch(){        Connection conn = null;        PreparedStatement preparedstatement = null;        String sql = null;        try {            conn = TestTools.getConnection();            //事务开始            TestTools.beginTx(conn);            sql = "INSERT INTO customers VALUES(?,?,?)";            preparedstatement = conn.prepareStatement(sql);            Date date = new Date(new java.util.Date().getTime());            //开始计时            long begin = System.currentTimeMillis();            for(int i = 0; i < 100000; i++){                preparedstatement.setInt(1, (i+1));                preparedstatement.setString(2, "name_"+i);                preparedstatement.setDate(3, date);                //"积攒"300条记录之后一块提交到数据库                preparedstatement.addBatch();                if((i + 1) % 300 == 0){                    preparedstatement.executeBatch();//执行提交                    preparedstatement.clearBatch();//清除积攒的记录                }            }            //若总条数不是积攒数的整数倍,则需要额外的在执行一次,比如总条数400,积攒数300,则执行一次之后还有100            //条记录,100%300不等于0无法提交,所以需要再判断一下是否需要再提交一次。            if(100000 % 300 != 0){                preparedstatement.executeBatch();//执行提交                preparedstatement.clearBatch();//清除积攒的记录            }            //计时结束            long end = System.currentTimeMillis();            System.out.println("时间:"+(end - begin));            //事务提交            TestTools.commit(conn);        } catch (Exception e) {            e.printStackTrace();            //事务回滚            TestTools.rollback(conn);        }finally{            TestTools.release(preparedstatement, conn);        }    }}