无法使用for循环多次更新

时间:2015-05-16 06:21:50

标签: java mysql jdbc

我有一个表格可以将股票从branchA借给branchB。用户可以输入多个产品,我通过将文本区域用(“,”)分割来实现,并在其中放置一个数组。提交表单后,我将其插入表中,然后从另一个表中更新库存。这是我的代码(我实际放在JSP文件而不是普通的Java类中):

   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");
   }

第一个查询在表中成功插入了多行,但我的第二个查询只更新了数组中的第一个索引。我不知道我做错了什么,因为他们都在同一个循环中。

请帮忙吗?

更新:我做了这个.addBatch和.executeBatch并且它工作了一段时间,现在它再次无效。它只更新第一个索引。

1 个答案:

答案 0 :(得分:0)

尝试类似这样的代码片段(我可能会搞乱你的变量类型,因为我猜对了。

    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");
  }

并且不要忘记之后清理资源。在finally {}子句中关闭语句和连接。