我想根据表'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();
}
});
如果您发现上述代码有任何错误,请提供帮助或通知。
尝试使用其他语句,因为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();
}
});