• JDBC之批量处理


    JDBC之批量处理

    一、批量处理JDBC语句提高处理速度

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

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

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

      通常我们会遇到两种批量执行SQL语句的情况:
      ①多条SQL语句的批量处理;


      ②一个SQL语句的批量传参;

    二、示例代码

      1 /**
      2      * 使用批量处理
      3      */
      4     @Test
      5     public void testBatch() {
      6     Connection connection = null;
      7     PreparedStatement preparedStatement = null;
      8     String sql = null;
      9 
     10     try {
     11         connection = JDBCTools.getConnection();
     12         JDBCTools.beginTx(connection);
     13         sql = "INSERT INTO customers VALUES(?,?,?)";
     14         preparedStatement = connection.prepareStatement(sql);
     15         Date date = new Date(new java.util.Date().getTime());
     16 
     17         long begin = System.currentTimeMillis();
     18         for (int i = 0; i < 100000; i++) {
     19         preparedStatement.setInt(1, i + 1);
     20         preparedStatement.setString(2, "name_" + i);
     21         preparedStatement.setDate(3, date);
     22 
     23         // "积攒" SQL
     24         preparedStatement.addBatch();
     25 
     26         // 当 "积攒" 到一定程度, 就统一的执行一次. 并且清空先前 "积攒" 的 SQL
     27         if ((i + 1) % 300 == 0) {
     28             preparedStatement.executeBatch();
     29             preparedStatement.clearBatch();
     30         }
     31         }
     32 
     33         // 若总条数不是批量数值的整数倍, 则还需要再额外的执行一次.
     34         if (100000 % 300 != 0) {
     35         preparedStatement.executeBatch();
     36         preparedStatement.clearBatch();
     37         }
     38 
     39         long end = System.currentTimeMillis();
     40 
     41         System.out.println("Time: " + (end - begin)); // 569
     42 
     43         JDBCTools.commit(connection);
     44     } catch (Exception e) {
     45         e.printStackTrace();
     46         JDBCTools.rollback(connection);
     47     } finally {
     48         JDBCTools.releaseDB(null, preparedStatement, connection);
     49     }
     50     }
     51     
     52     /**
     53      * 使用PreparedStatement
     54      */
     55     @Test
     56     public void testBatchWithPreparedStatement() {
     57     Connection connection = null;
     58     PreparedStatement preparedStatement = null;
     59     String sql = null;
     60 
     61     try {
     62         connection = JDBCTools.getConnection();
     63         JDBCTools.beginTx(connection);
     64         sql = "INSERT INTO customers VALUES(?,?,?)";
     65         preparedStatement = connection.prepareStatement(sql);
     66         Date date = new Date(new java.util.Date().getTime());
     67 
     68         long begin = System.currentTimeMillis();
     69         for (int i = 0; i < 100000; i++) {
     70         preparedStatement.setInt(1, i + 1);
     71         preparedStatement.setString(2, "name_" + i);
     72         preparedStatement.setDate(3, date);
     73 
     74         preparedStatement.executeUpdate();
     75         }
     76         long end = System.currentTimeMillis();
     77 
     78         System.out.println("Time: " + (end - begin)); // 9819
     79 
     80         JDBCTools.commit(connection);
     81     } catch (Exception e) {
     82         e.printStackTrace();
     83         JDBCTools.rollback(connection);
     84     } finally {
     85         JDBCTools.releaseDB(null, preparedStatement, connection);
     86     }
     87     }
     88 
     89     /**
     90      * 向 Oracle 的 customers 数据表中插入 10 万条记录 
     91      * 测试如何插入, 用时最短. 1. 使用 Statement.
     92      */
     93     @Test
     94     public void testBatchWithStatement() {
     95     Connection connection = null;
     96     Statement statement = null;
     97     String sql = null;
     98 
     99     try {
    100         connection = JDBCTools.getConnection();
    101         JDBCTools.beginTx(connection);
    102 
    103         statement = connection.createStatement();
    104 
    105         long begin = System.currentTimeMillis();
    106         for (int i = 0; i < 100000; i++) {
    107         sql = "INSERT INTO customers VALUES(" + (i + 1) + ", 'name_" + i + "', '29-6月 -13')";
    108         statement.addBatch(sql);
    109         }
    110         long end = System.currentTimeMillis();
    111 
    112         System.out.println("Time: " + (end - begin)); // 39567
    113 
    114         JDBCTools.commit(connection);
    115     } catch (Exception e) {
    116         e.printStackTrace();
    117         JDBCTools.rollback(connection);
    118     } finally {
    119         JDBCTools.releaseDB(null, statement, connection);
    120     }
    121     }
    View Code

      通过代码,我们可以发现,很明显处理速度batch > PreparedStatement > Statement

    如果,您对我的这篇博文有什么疑问,欢迎评论区留言,大家互相讨论学习。
    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
    如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
    如果,您对我的博文感兴趣,可以关注我的后续博客,我是【AlbertRui】。

    转载请注明出处和链接地址,欢迎转载,谢谢!

      

  • 相关阅读:
    UML建模图
    Ubuntu选择软件源
    用于主题检测的临时日志(c5ac07a5-5dab-45d9-8dc2-a3b27be6e507
    【Android】不弹root请求框检测手机是否root
    android开机动画(bootanimation)
    UniversalImageLoader(异步加载大量图片)
    PHP字符串
    Android获取本机号码及运营商
    静态代码块、构造代码块、构造方法
    Android来电拦截及来电转移
  • 原文地址:https://www.cnblogs.com/albertrui/p/8421635.html
Copyright © 2020-2023  润新知