executeBatch批量處理問題

2021-08-27 23:42:36 字數 825 閱讀 5940

boss交給了我乙個任務,讀取檔案,將檔案中的資料以「|」為分割標誌,將分割出來的字段一一對應資料庫裡的字段插入裡面。

1.前面的簡單,首先io流讀取資料,然後將讀取出來的資料一條條遍歷,再分割一下完事。

上**:

public static void readtxtfile(string filepath) throws ioexception, sqlexception catch (sqlexception e) 

statement pstmt = null;

class.forname(drive);

conn = drivermanager.getconnection(dburl, dbuser, password);

conn.setautocommit(false);

pstmt = conn.createstatement();

for(int i=0;i1 && (i+1) % 1000 == 0)

}pstmt.executebatch();

pstmt.close();

conn.commit();

}catch(exception e)

}

要注意的問題是1 conn.setautocommit(false);         要關閉自動提交

2 pstmt = conn.createstatement();   要把ps放在迴圈外面,不然只會執行一條

3  pstmt.clearbatch();                        每次執行後要將ps clear一下,防止程式卡死及重複提交

prepareStatement的批量處理資料

preparestatement.addbatch 新增sql進入preparestatement中 preparestatement.executebath 批量執行sql 例子 preparedstatement ps null public void addflowcarddetail str...

executeBatch 如何得到是哪些語句出錯

connection conn null preparedstatement pst null resultset rs null intarr null intarrex null booleansuccess true try pst.setint 1 temp pst.addbatch arr...

執行executeBatch批處理遇到的問題

執行batch批處理遇到的問題 1.務必關掉自動提交 增強執行效率 conn.setautocommit false 2.executebatch失效問題 1 務必將語句pstmt conn.preparestatement sql 放到迴圈體外,否則只能插入一條語句 int size ipadre...