2011-11-02 51 views
0

我有一個讀取通知對象列表的函數。現在我必須分批讀取通知對象100(假設)並更新它。如何批量讀取列表

public boolean addBSInfoToTemp(List<ParentNotification> pNotify) 
    throws DaoException, BatchUpdateException { 

int cnt = 0; 

final String query = "insert into Temp values ?,?,?,'Y','N',?,?,?,?"; 

    while (!pNotify.isEmpty()) { 
    try { 
    pst = getConnection().prepareStatement(query); 
    for (ParentNotification pn : pNotify) { 
     cnt++; 
     pst.setInt(1, pn.getUserId()); 
     pst.setString(2, pn.getEmail()); 
     pst.setString(3, pn.getNotificationType()); 

     Date createdDate = (Date) pn.getCreatedDate(); 
     pst.setDate(4, createdDate); 

     pst.setString(5, pn.getCreatedBy()); 

     Date icsesCreatedDate = (Date) pn.getIcsesCreatedDate(); 
     pst.setDate(6, icsesCreatedDate); 

     pst.setString(7, pn.getMiscellaneous()); 

     pst.addBatch(); 
     if(cnt==batchCount){ 
     break; 
     } 
    } 

    int[] batch = pst.executeBatch(); 
    if (batch.length != 0) { 
     flag = true; 
    } 
    } catch (BatchUpdateException b) { 
    flag = false; 
    } catch (SQLException sqlx) { 
    flag = false;   
    } finally { 
    close(pst, null); 
    } 
} 
return flag; 

}

我所試圖做的是閱讀列表與batchCount = 100然後更新並返回101條記錄,然後更新其他100個記錄,直到列表爲空。

回答

0

你有這樣的:

而(pNotify.isEmpty()!){

,但我沒有看到你曾經從列表中刪除的對象,所以你有無限循環。我只想讓外循環來遍歷所有這樣的元素:

for (int i=0; i<=pNotify.size(); i++){ 

// and inside check if it is devisible by batch length 

     if(i % batchCount == 0){ 
     break; 
     } 

} 
+0

是的,但我應該能夠從101重新恢復,如果batchCount = 100 – Naveen

+0

應該是我,而不是CNT,你不不需要單獨計數。修復了代碼。 –