根据另一张表的选定输出来更新另一张表中的行,但程序仅更新最上面的一行

问题描述 投票:0回答:1

我想根据表'BRTR'中的选定行来更新表'WORKTR'中的多行。不幸的是,该程序仅基于“ BRTR”表的第一行来更新WORKTR表。

这是我的代码:

      Button Back = (Button) findViewById(R.id.btn_back);
        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                connectionClass = new ConnectionClass();
                Connection con = connectionClass.CONN();
                try {
                    final String PIC = lblname.getText().toString();
                    String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
                    String check = "Select * from BRTR where work_date='" + dates + "' and end_break is null";
                    Statement st = con.createStatement();

                    rs = st.executeQuery(check);


                    while (rs.next()) {
                        String PF_NO = rs.getString("PF_No");



                        String query2 = "Update WORKTR set status ='" + b_status + "' where Start_date='" + dates + "' and Pf_no='" + PF_NO + "' and Scan_by='" + PIC + "' and status='" + a_status + "'";

                        st.executeUpdate(query2);
                    }
                } catch (Exception ex)
                {

                }
                onBackPressed();
            }

        });

如果您发现上述代码有任何错误,请提供帮助或通知。

java android-studio jdbc sql-update rows
1个回答
0
投票

尝试使用其他语句,因为st.executeUpdate(query2);将关闭ResultSet,并且您将获得JdbcSQLException,但是您将忽略代码上的所有异常。您至少应将ex.printStackTrace()放入catch块,以便了解发生了什么情况。

虽然在代码方面有很多改进,但从功能上来说,这应该对您有用,

      Button Back = (Button) findViewById(R.id.btn_back);
    Back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            connectionClass = new ConnectionClass();
            Connection con = connectionClass.CONN();
            try {
                final String PIC = lblname.getText().toString();
                String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
                String check = "Select * from BRTR where work_date='" + dates + "' and end_break is null";
                Statement st = con.createStatement();

                rs = st.executeQuery(check);


                while (rs.next()) {
                    String PF_NO = rs.getString("PF_No");



                    String query2 = "Update WORKTR set status ='" + b_status + "' where Start_date='" + dates + "' and Pf_no='" + PF_NO + "' and Scan_by='" + PIC + "' and status='" + a_status + "'";

                    con.createStatement().executeUpdate(query2);
                }
            } catch (Exception ex)
            {
               ex.printStackTrace(); // now we can see what is going wrong
            }
            onBackPressed();
        }

    });
© www.soinside.com 2019 - 2024. All rights reserved.