I have a form that borrows stocks from branchA to branchB. The user can input multiple products and I do this by having a text area split it with (","), and place in it an array. After submitting my form, I insert it in a table and then update the stocks from another table. Here is my code (which I actually placed in a JSP file instead of a normal Java class):
try {
for (int iCtr=0; iCtr<idsplit.length; iCtr++) {
sInsertQuery = "INSERT INTO PULLOUT_REPORTS (control_number, dateofpullout, item_des, item_size, item_qty, pullout_from, pullout_to) values ('"+cn+"','"+po+"','"+idsplit[iCtr]+"','"+issplit[iCtr]+"','"+qtsplit[iCtr]+"','"+fr+"','"+to+"')";
pInsertPullout = conn.prepareStatement(sInsertQuery);
pInsertPullout.executeUpdate();
if (fr.equals("Antipolo") && to.equals("Binangonan")) {
sUpdateRecord = "UPDATE maintable SET antip_qty = antip_qty - ? WHERE item_code = ? AND item_size = ?";
pUpdateFrom = conn.prepareStatement(sUpdateRecord);
pUpdateFrom.setString(1,qtsplit[iCtr]);
pUpdateFrom.setString(2,idsplit[iCtr]);
pUpdateFrom.setString(3,issplit[iCtr]);
pUpdateFrom.addBatch();
pUpdateFrom.executeBatch();
sUpdateRecord1 = "UPDATE maintable SET binang_qty = binang_qty + ? WHERE item_code = ? AND item_size = ?";
pUpdateTo = conn.prepareStatement(sUpdateRecord1);
pUpdateTo.setString(1,qtsplit[iCtr]);
pUpdateTo.setString(2,idsplit[iCtr]);
pUpdateTo.setString(3,issplit[iCtr]);
pUpdateTo.addBatch();
pUpdateTo.executeBatch();
} }
}
catch (Exception e) {
response.sendRedirect("error.jsp");
}
The first query successfully inserts multiple rows in the table but my second query only updates the first index from the array. I don't know what I could be doing wrong since they're both inside the same loop.
Any help please?
UPDATE: I did this .addBatch and .executeBatch and it work a while, now it doesn't work again. It only updates the first index.