三种JDBC批量插入编程方法的比较

来源:互联网 发布:axure7.0 for mac破解 编辑:程序博客网 时间:2024/05/18 18:03

JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等。 
我用MySQL 5.1.5的JDBC driver 分别对三种比较常用的方法做了测试 

  • 方法一,使用PreparedStatement加批量的方法
    try {         Class.forName("com.mysql.jdbc.Driver");         conn = DriverManager.getConnection(o_url, userName, password);         conn.setAutoCommit(false);         String sql = "INSERT into tbl_person (name,age) VALUES(?,?)";         PreparedStatement prest = conn.prepareStatement(sql);         for(int x = 0; x < size; x++){            prest.setString(1, "张三");            prest.setString(2, 20);            prest.addBatch();         }         prest.executeBatch();         conn.commit();         conn.close();   } catch (SQLException ex) {      Logger.getLogger(MyLogger.class.getName()).log(Level.SEVERE, null, ex);   } catch (ClassNotFoundException ex) {        Logger.getLogger(MyLogger.class.getName()).log(Level.SEVERE, null, ex);   }  
    • 方法二 使用Statement加批量的方法
      conn.setAutoCommit(false);    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);    for(int x = 0; x < size; x++){      stmt.addBatch("INSERT INTO adlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3', 'localhost','20081009',8,'23123')");    }   stmt.executeBatch();   conn.commit();  

      方法三:直接使用Statement
      conn.setAutoCommit(false);   Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,                                       ResultSet.CONCUR_READ_ONLY);   for(int x = 0; x < size; x++){      stmt.execute("INSERT INTO adlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3', 'localhost','20081009',8,'23123')");   }   conn.commit();  
      使用上述方法分别插入10万条数据的平均测试时间为: 
      方法一:17.844
      方法二:18.421
      方法三:16.359

      可以看出JDBC的batch语句插入不但没有性能提升,反而比没有用batch的时候要慢,当然这可能跟JDBC具体驱动的实现方法有关。 附件中是我测试代码,可以用来在自己电脑上跑一下。 

      在执行批量插入的时候最主要的是将自动提交取消,这样不管是否用JDBC的batch语法应该都没有关系。 
    conn.setAutoCommit(false)  
    个人觉得第一种方法是最方便最实用的。


1 0
原创粉丝点击