java 批量插入 Oracle

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