2012-10-11 89 views
0

我有兩個ArrayList,我必須插入到數據庫中。我有用於在數據庫中插入一個ArrayList值的代碼...此處是我在數據庫中插入值第一的ArrayList現在如何在同一查詢中同時插入兩個Arraylist值

for (int j = 0; j < list.size(); j++) { 
    int d = (int) list.get(j); 
    stmt.executeUpdate("insert into cdrcost (calldate) value ('" + d+ "')); 
} 

按我的需要我還有一個ArrayList中插入到我提到here.So相同的查詢,我需要的任何路徑中的數據庫,這樣的價值觀這兩個arraylist插入數據庫.. 任何幫助將不勝感激... Thanx提前...

+0

你是什麼意思與「同一個查詢中」?您可以將上面的代碼用於這兩個列表。你有什麼問題? –

回答

3
PreparedStatement psth = dbh.prepareStatement("insert into cdrcost (calldate) value (?)"); 
for (List<Integer> lst: Arrays.<List<Integer>>asList(list1,list2)) 
    for (int value: lst) { 
    psth.setInt(1,value); 
    psth.addBatch(); 
    } 
psth.executeBatch(); 

如果您需要設置大於1項的值更多:

PreparedStatement psth = dbh.prepareStatement("insert into cdrcost (calldate, othercolumn) value (?, ?)"); 
Iterator<Integer> it1 = list1.iterator(); 
Iterator<Integer> it2 = list2.iterator(); 
for (; it1.hasNext() && it2.hashNext();) { 
    psth.setInt(1,it1.next()); 
    psth.setInt(2,it2.next()); 
    psth.addBatch(); 
} 
psth.executeBatch(); 
+0

+1批量插入。 –

+0

@MiljenMikic謝謝先生的快速響應..我有多個列和多個arraylist然後如何插入..sir PLZ指導在這種情況下 – Ram

+0

我不跟着你。你有2個數組列表,必須使用它來提供查詢「insert into sometable(column1,column2)values(valueFromList1,valueFromList2)」? – jdevelop

相關問題