简体   繁体   English

多次更新仅适用于 jdbc 中的一个查询

[英]Multiple update only works on one of the query in jdbc

I have a section in my application which require to update two tables at the same time (WORKTR and BRTR).我的应用程序中有一个部分需要同时更新两个表(WORKTR 和 BRTR)。

However, the program only works on the first table update query while the second query did not work, resulting only a table been updated.但是,该程序仅适用于第一个表更新查询,而第二个查询不起作用,导致仅更新了一个表。 There are no error prompt at the Log error. Log错误处没有错误提示。

Here is my jdbc code to update both table:这是我更新两个表的 jdbc 代码:

                String query=("UPDATE WORKTR SET status = ? WHERE Pf_no = ? AND status = ? AND Scan_by= ? AND Start_date= ?");
                            PreparedStatement ps = con.prepareStatement(query);
                            // set the preparedstatement parameters
                            ps.setString(1,a_status);
                            ps.setString(2,OperatorPF);
                            ps.setString(3,b_status);
                            ps.setString(4,PIC);
                            ps.setString(5,dates);

                            // call executeUpdate to execute our sql update statement
                            ps.executeUpdate();

                            if (ps.executeUpdate()==1) {

                                String queryBRTR= ("Update BRTR set end_break = ?, status= ? where  and PF_No= ? and work_date= ?");
                                PreparedStatement br = con.prepareStatement(queryBRTR);
                                // set the preparedstatement parameters
                                br.setString(1,currentTime);
                                br.setString(2,a_status);
//                                br.setString(3,b_status);
                                br.setString(3,OperatorPF);
                                br.setString(4,dates);
                                // call executeUpdate to execute our sql update statement

                                br.executeUpdate();


                                if( br.executeUpdate()==1){

                                    z="You may resume your work now";
                                }
                                else{
                                    ps.close();
                                    br.close();
                                }

Please highlight if there is any mistake or suggest another way that would work to update both table.请强调是否有任何错误或建议另一种方法来更新这两个表。

Change to改成

int rowsChanged = ps.executeUpdate();

if (rowsChanged  >= 1) {

You were calling executeUpdate twice.您两次调用executeUpdate

  • Should not re-execute the PreparedStatement不应重新执行 PreparedStatement
  • Status has changed so even if you could re-use PreparedStatement , the update would probably fail状态已更改,因此即使您可以重新使用 PreparedStatement ,更新也可能会失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM