ResultSet.updateRow() 更改未反映在 JTextFields 中,直到我重新运行我的程序
ResultSet.updateRow() changes not reflected in JTextFields until I rerun my program
我正在关注 this tutorial,到目前为止这对我很有帮助。我正在制作一个 GUI 以在本地托管的 Java derby 数据库上执行 CRUD 操作。
这是我的问题:
- 我在 JTextFields 中输入新数据,然后按下 "Update" 按钮
- 我将新的 JTextField 数据保存到本地字符串变量
- 我使用结果集对象 rs 通过 rs.updateRow()
将更改提交到数据库
- 更改是在数据库中进行的,但仅在我关闭并重新运行我的程序时才显示在我的 JTextFields 中。
如何使我的 JTextFields 更新以反映新数据而无需关闭并重新运行程序?
private void btnUpdateRecordActionPerformed(java.awt.event.ActionEvent evt) {
String customerID = textCustomerID.getText();
int newCustomerID = Integer.parseInt(customerID);
String name = textName.getText();
String address1 = textAddress1.getText();
String address2 = textAddress2.getText();
String phone = textPhone.getText();
String email = textEmail.getText();
try{
rs.updateInt("CUSTOMER_ID", newCustomerID);
rs.updateString("NAME", name);
rs.updateString("ADDRESSLINE1", address1);
rs.updateString("ADDRESSLINE2", address2);
rs.updateString("PHONE", phone);
rs.updateString("EMAIL", email);
rs.updateRow();
JOptionPane.showMessageDialog(this, "Updated");
} catch(SQLException err){
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
我认为您需要 运行 使用 SwingUtilities.invokeLater
进行更新。 Swing GUI 的更改是在另一个线程 EDT 上进行的。
ResultSet
接口还包含一个方法,如果 ResultSet 对更改敏感,则用数据库更改更新行。
rs.refreshRow();
refreshRow()
--> 用数据库中的最新值刷新该行的列值。
在 rs.updateRow()
之后继续并使用更新的数据设置 JTexFields。喜欢jtextfield1.setText(rs.getString("ColumName"))
我正在关注 this tutorial,到目前为止这对我很有帮助。我正在制作一个 GUI 以在本地托管的 Java derby 数据库上执行 CRUD 操作。
这是我的问题:
- 我在 JTextFields 中输入新数据,然后按下 "Update" 按钮
- 我将新的 JTextField 数据保存到本地字符串变量
- 我使用结果集对象 rs 通过 rs.updateRow() 将更改提交到数据库
- 更改是在数据库中进行的,但仅在我关闭并重新运行我的程序时才显示在我的 JTextFields 中。
如何使我的 JTextFields 更新以反映新数据而无需关闭并重新运行程序?
private void btnUpdateRecordActionPerformed(java.awt.event.ActionEvent evt) {
String customerID = textCustomerID.getText();
int newCustomerID = Integer.parseInt(customerID);
String name = textName.getText();
String address1 = textAddress1.getText();
String address2 = textAddress2.getText();
String phone = textPhone.getText();
String email = textEmail.getText();
try{
rs.updateInt("CUSTOMER_ID", newCustomerID);
rs.updateString("NAME", name);
rs.updateString("ADDRESSLINE1", address1);
rs.updateString("ADDRESSLINE2", address2);
rs.updateString("PHONE", phone);
rs.updateString("EMAIL", email);
rs.updateRow();
JOptionPane.showMessageDialog(this, "Updated");
} catch(SQLException err){
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
我认为您需要 运行 使用 SwingUtilities.invokeLater
进行更新。 Swing GUI 的更改是在另一个线程 EDT 上进行的。
ResultSet
接口还包含一个方法,如果 ResultSet 对更改敏感,则用数据库更改更新行。
rs.refreshRow();
refreshRow()
--> 用数据库中的最新值刷新该行的列值。
在 rs.updateRow()
之后继续并使用更新的数据设置 JTexFields。喜欢jtextfield1.setText(rs.getString("ColumName"))