Search code examples
javasqlderby

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

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

Solution

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

    rs.refreshRow();
    

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