Saving changes from a QueryDataSet

You can use different Resolver implementations to save changes back to your data source. QueryDataSets use a QueryResolver to save changes by default. The default resolver can be overridden by setting the StorageDataSet.resolver property. When data is provided to the data set, the StorageDataSet tracks the row status information (either deleted, inserted, or updated) for all rows. When data is resolved back to a data source like a SQL server, the row status information is used to determine which rows to add to, delete from, or modify in the SQL table. When a row has been successfully resolved, it obtains a new row status of resolved (either RowStatus.UPDATE_RESOLVED, RowStatus.DELETE_RESOLVED, or RowStatus.INSERT_RESOLVED). If the StorageDataSet is resolved again, previously resolved rows will be ignored, unless changes have been made subsequent to previous resolving.

This topic explores the basic resolver functionality provided by the DataExpress package. It extends the concepts explored in the "Querying a database" tutorial to the resolving phase where you save your changes back to the data source.

To step through this tutorial, use the files you created from the "Querying a database" tutorial, or start with the completed sample files, located in the /jbuilder/samples/DataExpress/QueryProvider directory.

The "Querying a database" tutorial explored the providing phase where data is obtained from a data source. The tutorial instantiated a QueryDataSet and associated UI components, and displayed the data retrieved from the JDataStore employee sample. The Save button on the JdbNavToolBar can be used to save data changes back to the employee file. In the next topic, we add a button that also performs basic resolving code. When either the custom button or the toolbar's Save button is pressed, the changes made to the data in the QueryDataSet are saved to the employee data file using the QueryDataSet's default QueryResolver.

Tutorial: adding a button to save changes from a QueryDataSet

To create this application,

  1. Open the project file you created for the "Querying a database" tutorial by choosing File|Open, and then browsing to that project. If you did not complete the tutorial, you can access the completed project files from the samples/DataExpress/QueryProvider sub-directory of your JBuilder installation.

    Note: You should make backup copies of these files before modifying them since other tutorials in this book use the "Querying a database" files as a starting point.

  2. Select the Frame file in the Content pane.

  3. Add a JButton component from the Swing tab of the component palette. Set the button's text property to Save Changes. (See the finished application at the beginning of this tutorial for general placement of the controls in the UI.)

  4. Make sure the JButton is still selected, then click the Events tab of the Inspector. Select, then double-click the actionPerformed() method. This changes the focus of the AppBrowser from the UI designer to the Source pane and displays the stub for the actionPerformed() method.

    Add the following code to the actionPerformed() method:

      try {
          database1.saveChanges(queryDataSet1);
          System.out.println("Save changes succeeded");
      }
      catch (Exception ex) {
    // displays the exception on the JdbStatusLabel if the
    // application includes one,
    // or displays an error dialog if there isn't 
        DBExceptionHandler.handleException(ex);  }
    
    If you've used different names for the instances of the objects, for example, database1, replace them accordingly.

  5. Run the application by selecting Run|Run Project. The application compiles and displays in a separate window. Data is displayed in a table, with a Save Changes button, the toolbar, and a status bar that reports the current row position and row count.

    If errors are found, an error pane appears that indicates the line(s) where errors are found. The code of the custom button is the most likely source of errors, so check that the code above is correctly entered. Make corrections to this and other areas as necessary to run the application.

When you run the application, notice the following behavior:

Make changes to the data displayed in the table by inserting, deleting, and updating data. You can save the changes back to the server choosing either,

Note: Because of data constraints on the employee table, the save operation may not succeed depending on the data you change. Since other edits may return errors, make changes only to the FIRST_NAME and LAST_NAME values in existing rows until you become more familiar with the constraints on this table.