We help IT Professionals succeed at work.

addBatch problem in PreparedStatement

cofactor
cofactor asked
on
Here is a small code attached .

This code  loops 3 times.

 In eclipse debugger , when I  inspect  the pstmt1  I see only  the last loop  values.

Whats wrong with this code ?   why all the queries are not added into batch ?  

System: mysql5   and mysql-connector-java-5.1.14-bin.jar , jboss 5.0
addBatch.txt
Comment
Watch Question

I Didn't see pstmt.executeBatch() after for loop in your code.


pstmt.executeBatch();

Awarded 2011
Awarded 2011
Commented:
prepareStatement should be outside the loop

pstmt1= conn.prepareStatement(INSERT_INTO_TBL_T_FF_REQUEST);

this should be outside the loop
						pstmt1= conn.prepareStatement(INSERT_INTO_TBL_T_FF_REQUEST);

for (String breed : ffgroupVO.getBreedTypeId()) {
						pstmt1.setString(1, group_application_Number);
						pstmt1.setInt(2, Integer.parseInt(breed));
						String quantity=ffgroupVO.getRequiredQuantity().get(i);
						pstmt1.setInt(3, Integer.parseInt(quantity));
						pstmt1.setString(4, null);
						pstmt1.setString(5, null);
						pstmt1.setString(6,userVO.getCitizenId());
						pstmt1.setTimestamp(7, CommonUtil.currentTime());
						pstmt1.setString(8, null);
						pstmt1.setString(9, null);
						pstmt1.addBatch();
						i=i+1;
				
				}

// Execute the batch
    int [] updateCounts = pstmt.executeBatch();

Open in new window

Awarded 2011
Awarded 2011

Commented:

pstmt1= conn.prepareStatement(INSERT_INTO_TBL_T_FF_REQUEST);

it should be before the lopp - you carete every time new and
so it has only one set added

Author

Commented:
excellent