Queries

Introduction

The purpose of this section is to explain the concept of query objects, as used in the Wizard.

A query is meant provide the same kind of functionality as a database view.

The wizard breaks down a query into columns. The nodes on the left show a database query and its associated columns.

Each node in the tree has a tab associated with it, which can be used to view, and alter the information displayed.

 


Creating A Query

Please ensure that the Wizard is connected to the database, and that database scanning is on.

When a "Queries" node is selected in the tree, a "Queries View" tab appears in the right pane of the wizard. Below is an example.

When the create button is pressed, the query is executed, this validates it.


Editing A Query

Once a query is created, it is possible to change the SQL, changes appear in the expanded tree. New columns, however, just appear, they do not have special markers.

Once a query has been changed, and the columns updated to reflect this, all the classes should be checked. This ensures they pick up column changes. All class methods should also be checked to ensure parameters do not refer to old columns.


Creating Query Classes

When the "JDBC2 Classes" node is clicked, the tab below is displayed. This tab allows a new Query class to be created.

The Database class generated contains the methods which use JDBC to select the data, and to navigate the ResultSet.

The Data class generated contains data for one ResultSet.

If the Serializable data class checkbox is checked, the generated data class implements java.io.Serializable.

The wizard typically generates two classes. A Data class and a JDBC class.

Be sure to call close() in the generated JDBC class. In JDBC2, prepared statements can be reused. Hence the wizard does not close them automatically.


Editing A Query Class

When a specific query node is selected in the tree, the following set of tabs is displayed:

Class Info Tab

Once a class has been created, we can drill down the tree to its node. Selecting a class' node brings up a tabbed pane, whose first tab is illustrated below

The class information tab allows us to change general class information.

Now we should review the table columns the classes are to encapsulate. For example, which are used to get data, insert row and update row calls on the query's resultset.


Columns Tab, Data Class

The columns tab shows all the columns the database object has to offer. In effect it allows us to see which columns are mapped to java properties of the generated data class, and what SQL operations we wish to perform on that column.

JDBC2 allows resultsets to be updatable. The wizard allows inividual columns to be earmarked for Select, Update, and Insert. The code generated in the database class can have methods to select, insert and update the ResultSet (JDBC2Query Settings tab sets the concurrency to read only or read/write). Usually the query must only select from one table for this to work.

Columns which have an access of  "--- None ---" will not be present in the data class.

By double clicking on any cell, a property editor dialog for that data class member pops up.


The Database Class

The following SQL methods have been created:

SQL Method In Detail

When a SQL Method is selected, and the "Edit..." button is pressed, a method edtior pops up.

The method is given a name. This name will generate a method to execute the query in the database class.

The SQL is completed here. Notice how we have completed the SQL here by entering "AND EMPNO = ?".

Parameters are added, one per the "?" present in the SQL, and in the same order. The parameter editor appears below.

Typically, you would provide a useful name (the default is not very useful). Then select the dbms type of the parameter. At this point all other combos reflect the default settings for this data type. The precision and scale should be entered. And that should be it.


JDBC2 Query Settings

By default the generated code is read only on the resultset. That is to say, no insert, update or delete methods are generated.

Advanced Settings

The advanced settings tab provides additional control over the generated classes. The wizard generates two classes.

For the data class the following settings are available:

For the database class, the following settings are available:


Using The Generated Code

Using query classes is pretty much the same as other JDBC2 classes generated by the wizard. The only difference being that no insert/update or delete methods are available.

Insert/update and delete is only possible using the generated resultset methods: insertRow(), updateRow() and deleteRow().

Using JDBC2 classes is described here.