Reputation: 45
I am doing like this but in database last line only update.
I have checked if 20 rows there, all rows are adding at time of addbatch but at the time of executeBatch, in database upadated only last rows only.
ArrayList<String> li = (ArrayList<String>)request.getAttribute("VL01Data");
Iterator<String> it = li.iterator();
String splitter = request.getAttribute("SPLITTER") != null ?(String)request.getAttribute("SPLITTER"): "!";
String QueryIn = "INSERT INTO ......) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
while(it.hasNext()) {
String value =it.next().toString();
StringTokenizer strTok = new StringTokenizer(value,splitter);
String S = strTok.nextElement().toString().trim();
String I = strTok.nextElement().toString().trim();
String M = strTok.nextElement().toString().trim();
String d = strTok.nextElement().toString();
String R = strTok.nextElement().toString().trim();
String Pu = strTok.nextElement().toString().trim();
.
.
pstmt = con.prepareStatement(QueryIn);
pstmt.setString(1,e );
pstmt.setString(2, r);
pstmt.setString(3, y);
pstmt.setString(4, d);
pstmt.setString(5, y);
pstmt.setString(6, i);
.
.
pstmt.setString(19,f1 );
pstmt.setString(20,f2);
pstmt.addBatch();
}
pstmt.executeBatch();
con.commit();
Upvotes: 0
Views: 384
Reputation: 201487
Don't re-prepare
the statement in the loop body. Prepare it once before your loop like
pstmt = con.prepareStatement(QueryIn);
while(it.hasNext()) {
String value = it.next().toString();
StringTokenizer strTok = new StringTokenizer(value,splitter);
String S = strTok.nextElement().toString().trim();
String I = strTok.nextElement().toString().trim();
String M = strTok.nextElement().toString().trim();
String d = strTok.nextElement().toString();
String R = strTok.nextElement().toString().trim();
String Pu = strTok.nextElement().toString().trim();
// pstmt=con.prepareStatement(QueryIn);
// ...
when you re-assign the pstmt
reference by calling prepareStatement
it loses the previous (batch) state (on every loop iteration), so you only get the very last one when you call executeBatch()
.
Upvotes: 1