4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3211403
  • 169
  • 1
  • 8

1 Answers1

0

Try something like this snippet (i might ve messed up with types of your variables, because I guessed them.

    try {
     sInsertQuery = "INSERT INTO PULLOUT_REPORTS " +
                    "(control_number, dateofpullout, item_des, item_size, item_qty, pullout_from, pullout_to)" +
                    " values " +
                    "(?,?,?,?,?,?,?)";
      pInsertPullout = conn.prepareStatement(sInsertQuery);
      sUpdateRecord = "UPDATE maintable SET antip_qty = antip_qty - ? WHERE item_code = ? AND item_size = ?";
      pUpdateFrom = conn.prepareStatement(sUpdateRecord);
      sUpdateRecord1 = "UPDATE maintable SET binang_qty = binang_qty + ? WHERE item_code = ? AND item_size = ?";
      pUpdateTo = conn.prepareStatement(sUpdateRecord1);

      for (int iCtr=0; iCtr<idsplit.length; iCtr++) {
          pInsertPullout.setLong(1, cn);
          pInsertPullout.setLong(2, po);
          pInsertPullout.setString(3, idsplit[iCtr]);
          pInsertPullout.setString(4, issplit[iCtr]);
          pInsertPullout.setString(5, qtsplit[iCtr]);
          pInsertPullout.setString(6, fr);
          pInsertPullout.setString(7, to);

          pInsertPullout.executeUpdate();
          pInsertPullout.clearParameters();

            if (fr.equals("Antipolo") && to.equals("Binangonan")) {
                      pUpdateFrom.setString(1,qtsplit[iCtr]);
                      pUpdateFrom.setString(2,idsplit[iCtr]);
                      pUpdateFrom.setString(3,issplit[iCtr]);
                      pUpdateFrom.addBatch();


                      pUpdateTo.setString(1,qtsplit[iCtr]);
                      pUpdateTo.setString(2,idsplit[iCtr]);
                      pUpdateTo.setString(3,issplit[iCtr]);
                      pUpdateTo.addBatch();

                  } 
      }
      pUpdateFrom.executeBatch();
      pUpdateTo.executeBatch();
  }

  catch (Exception e) {
     response.sendRedirect("error.jsp");
  }

and don't forget to cleanup resources afterwards. Close statements and connection in finally{} clause.

Dmitry V.
  • 336
  • 4
  • 11