java 批量插入 Oracle

  1. sql = "INSERT INTO LOG_FILENAME(ID,FILENAME,CREATETIME) VALUES(2,?,sysdate)";  
  2.   
  3. public void batchInsertFileNames(File[] files) throws SQLException {  
  4.         Connection conn = null;  
  5.         PreparedStatement pstmt = null;  
  6.         try {  
  7.             conn = dataSource.getConnection();  
  8.             pstmt = (PreparedStatement) conn.prepareStatement(sql);  
  9.             Date date = new Date();  
  10.             for (int i = 0; i < files.length; i++) {  
  11.                 setParams(pstmt, files[i].getName(), date);  
  12.             }  
  13. //下句执行后开始批量插入数据  
  14.             pstmt.executeBatch();  
  15.         } finally {  
  16.             DbUtils.close(pstmt);  
  17.         }  
  18. }  
  19.   
  20.     private void setParams(PreparedStatement pstmt, String fileName, Date date)  
  21.             throws SQLException {  
  22.         pstmt.setString(1, fileName);  
  23.                   //addBatch();执行后暂时记录此条插入  
  24.         pstmt.addBatch();  
  25.     }  
posted @ 2017-05-24 16:52  update_  阅读(2344)  评论(0编辑  收藏  举报