Filtering, sorting, and locating data

Database application development is a feature of JBuilder Professional and Enterprise. Distributed application development is a feature of JBuilder Enterprise.

Once you've completed the providing phase of your application and have the data in an appropriate DataExpress package DataSet component, you're ready to work on the core functionality of your application and its user interface. This chapter demonstrates the typical database application features of filtering, sorting, and locating data.

A design feature of the DataExpress package is that the manipulation of data is independent of how the data was obtained. Regardless of which type of DataSet component you use to obtain the data, you manipulate it and connect it to controls in exactly the same way. Most of the examples in this chapter use the QueryDataSet component, but you can replace this with the TableDataSet or any StorageDataSet subclass without having to change code in the main body of your application.

Each sample is created using the JBuilder AppBrowser and design tools. Wherever possible, we'll use these tools to generate source Java code. Where necessary, we'll show you what code to modify in order to have your application perform a particular task.

These tutorials assume that you are comfortable using the JBuilder environment and do not provide detailed steps on how to use the user interface. If you're not yet comfortable with JBuilder, refer to the introductory tutorial in this manual or to the online help topic "Designing a user interface."

All of the following examples and tutorials involve accessing SQL data stored in a local database. These examples use the sample files in the downloadable Samples Pack, using the local JDataStore JDBC driver. For instructions on how to setup and configure JBuilder to use the sample JDataStore driver, see "Installing and setting up JBuilder for database applications".

We encourage you to use the samples as guides when adding these functions to your application. Finished projects and Java source files for many of these tutorials, with comments in the source file where appropriate, are provided. (If you downloaded JBuilder, you also need to download the Samples Pack.) All files referenced by these examples are found in the JBuilder samples directory. If you experience problems running the sample applications, see "JBuilder sample files" for information critical to this process.

Note: Some of the samples run only with JBuilder Enterprise.

To create a database application, you first need to connect to a database and provide data to a DataSet. Retrieving data for the tutorials" sets up a query that will be used for each of the following database tutorials. The following list of additional database functionality options (filter, sort, locate data) can be used in any combination, for example, you could choose to temporarily hide all employees whose last names start with letters between "M" and "Z". You could sort the employees that are visible by their first names.


Retrieving data for the tutorials

This topic provides the steps for setting up a basic database application that can be used with the tutorials in this chapter. The query that will be used in these tutorials is:
SELECT * FROM EMPLOYEE

This SQL statement selects all columns from a table named EMPLOYEE, included in the sample JDataStore.

To set up an application for use with the tutorials,

  1. Close all open projects (select File|Close Project).
  2. Select File|New Project.

  3. Enter a name and location for the project in the Project Wizard. Click Finish.

  4. Select File|New from the menu. Double-click the Application icon.

  5. Specify the package name and class name in the Application Wizard. Click Finish.

  6. Select the Design tab to activate the UI designer.
  7. Click the Database component on the Data Express tab of the component palette, then click in the component tree or the UI designer to add the component to the application.

    Open the connection property editor for the Database component by selecting, then clicking the connection property ellipsis in the Inspector. Set the connection properties to the JDataStore sample employee table as follows. The Connection URL points to a specific installation location. If you have installed JBuilder to a different directory, point to the correct location for your installation.
    Property name Value
    Driver com.borland.datastore.jdbc.DataStoreDriver
    URL Browse to your copy of /jbuilder/samples/JDataStore/datastores/employee.jds
    Username Enter your name
    Password not required

    The connection dialog includes a Test Connection button. Click this button to check that the connection properties have been correctly set. Results of the connection attempt are displayed in the status area. When the connection is successful, click OK. If the connection is not successful, make sure you have followed all the steps for "Connecting to a database".

  8. Add a QueryDataSet component to the designer by clicking on the QueryDataSet component on the Data Express tab and then clicking in the component tree or the UI Designer.

    Select the query property of the QueryDataSet component in the Inspector, click its ellipsis to open the QueryDescriptor dialog, and set the following properties:
    Property name Value
    Database database1
    SQL Statement SELECT * FROM EMPLOYEE

    Click Test Query to ensure that the query is runnable. When the status area indicates Success, click OK to close the dialog.

  9. Add a DBDisposeMonitor component from the More dbSwing tab. The DBDisposeMonitor will close the JDataStore when the window is closed.

  10. Set the dataAwareComponentContainer property for the DBDisposeMonitor to this.

To view the data in your application, add the following UI components and bind them to the data set as follows:

  1. Select contentPane(BorderLayout) in the component tree and set its layout property to null.
  2. Drop a JdbNavToolBarinto the area at the top of the panel in the UI designer. jdbNavToolBar1 automatically attaches itself to whichever DataSet has focus, so you do not need to set its dataSet property.

  3. Drop a JdbStatusLabel into the area at the bottom of the panel in the UI designer. jdbStatusLabel1 automatically attaches itself to whichever DataSet has focus, so you do not need to set its dataSet property.

  4. Add a TableScrollPane from the dbSwing tab to the center of the panel in the UI designer.

  5. Drop a JdbTable into the center of tableScrollPane1and set its dataSet property to queryDataSet1.

    You'll notice that the designer displays live data at this point.

  6. Select Run|Run Project to run the application and browse the data set.

The EMPLOYEE data set contains 42 records and 11 fields. In the status label for this application, you will see how many records are displaying. When the application is first run, the status label will read "Record 1 of 42". Some of the tutorials remove rows from a view. The status label will display the number of rows retrieved into the data set for each application.

For more information on retrieving data for your application, see the "Retrieving data from a data source" chapter.

The running application should look like this: