Patrick Patrick -5 years ago 93
Java Question

ResultSet.updateRow() changes not reflected in JTextFields until I rerun my program

I am following along with this tutorial which has been really helpful so far. I am making a GUI to perform CRUD operations on a locally hosted Java derby database.

Here's my problem:

  1. I type new data into my JTextFields and then I press my "Update" button

  2. I save the new JTextField data to local string variables

  3. I use my result set object rs to commit the changes to the database with rs.updateRow()

  4. The changes are made in the database but are only displayed in my JTextFields when I close and rerun my program.

How do I make my JTextFields update to reflect the new data without having to close and rerun the program?

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();

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


JOptionPane.showMessageDialog(this, "Updated");

} catch(SQLException err){
JOptionPane.showMessageDialog(this, err.getMessage());

Answer Source

ResultSet interface also contains a method to update a row with database changes, if the ResultSet is sensitive to change.


refreshRow() --> Refreshes the column values of that row with the latest values from the database.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download