JDBC Classes

When mapping a database table or view to java classes, it is necessary to create classes in the wizard. The wizard usually generates two classes for a selected database object. One class, "JDBC class", contains the JDBC code to access the database. The other, "data class", class holds the column values to use for a single database record.

Once a class is created in the wizard, we can specify the database object's columns we want the class to use. And we can get the wizard to create methods to select, update, insert and delete records in the database object.

It is worth noting that if we want to generate two selects, one with a customer's id, name and surname only, and another with all the customer table's columns, two classes need to be generated for the customer table. This affords cleaner object-relational mapping.

There now follows a description of how the wizard is used to create classes.


JDBC Classes View

The JDBC classes view allows a new JDBC class description to be created in the project file.

The database class is used by your application to select, insert, delete and update the database.

The data class is used by your application and the database class. The data class holds the data either retrieved from the database, or data to insert/update the database.

It is possible to extend the generated database class, and data class, with any class of your choice. Care should be taken when extending other classes, as there may be compilation errors which are due to problems/incompatibilities with the class being extended. when entering an extended class name, the full package and name should be entered, for example "com.j3.somePackage.ClassName".

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


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 in select, insert and update calls. This will determine the members of the generated data class, and how the JDBC SQL calls should be generated.


Columns Tab

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.

Only 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.

Column Property Editor

Below is the columns' view of the class. Defaults can be changed from here by double clicking on any cell in the table. In most cases only the access for a few columns will need  their access changed to specify which columns to use in the SQL select, update, insert statements, and hence which ones appear in the generated data class.

Columns earmarked for insert and update can use server side expressions to set column values. In the above example (Oracle), whenever a record is inserted, the Database server's date is used as a value for the column.

The expressions may not contain '?' characters.

Now we are ready to create JDBC methods to select, update, insert and delete records in the table.


SQL Methods Tab

Below is a snapshot of an example SQL Methods tab:

From this tab we can specify new database access methods, delete them, or modify existing methods. 

The methods will appear in the generated JDBC class. Most methods will use the generated data class to hold the data retrieved from the database, or to hold the data to update and insert records.

Select Method Editor

The JDBC class generated will have a method of the form

public void selectByCustomerID(Connection theConnection, String theCustomerID) throws SQLException

In order to specify the above, the following Select Statement Editor is used:

Update Method Editor

The JDBC class generated will have a method of the form

public void updateByCustomerID(Connection theConnection, CustomersData theUpdateData, String theCustomerID) throws SQLException

in order to specify this the following Update Statement Editor is used:

Delete Method Editor

The JDBC class generated will have a method of the form "public void deleteByCustomerID(Connection theConnection, String theCustomerID) throws SQLException", in order to specify this the following Delete Statement Editor is used:

 

Insert Method Editor

The JDBC class generated will have a method of the form "public void insert(Connection theConnection, CustomersData theInsertData) throws SQLException", in order to specify this the following Insert Statement Editor is used:

Method Parameter Editor

When adding a new parameter, or editing an existing one to a select, update or delete method, the following dialog pops up:

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.

The precision and scale are not used by the wizard at present, but a future version may well provide additional functionality based on the precision and scale values.


Advanced Settings

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

For the database class, the following settings are available:

For the data class the following settings are available:


JDBC2 Settings

When creating a JDBC2 class, the tab shown below is available:

The above settings apply to all the "Select" SQL methods defined for this class.

The methods generated by the wizard depend on the type of concurrency (read only or read write) and scrolling (forward only or insensitive/sensitive). The wizard generates resultset update methods only if the concurrency is read/write and scrillong is not scroll forward only.


Using The Generated Code

The following link explains how to use the JDBC1 code generated by the wizard.

The following link explains how to use the JDBC2 code generated by the wizard.


Copyright © 2002 J3 Limited, all rights reserved.