> List of tutorials for developing a Fat-Client in Java


React to the 'Store' button and save the data in the database - Fat-Client-Development

* For this document and all references (links) please obey the hints and regulations concerning copyright, disclaimer and trademarks.

  • The owner of this web-site (www.javascout.biz) is not responsible for the content of web-sites linked within this document or linked within other documents of www.javascout.biz.

  • If this document or other documents of this web-site (www.javascout.biz) infringes your rights or you think that rights of others (third parties) are infringed, please inform the author.
    An e-mail can be sent by clicking onto the 'hungry mailbox' in the upper right corner.

Last revision of this document:
2006-09-28

As the classes for the Business Object (BO) and the Data-Base-Access (DBA) Objects are implemented, they can be used to save entered data.

This step describes, how the application reacts to the click onto the button 'Store'; i.e. how the data from the GUI-elements is transferred to the BO and how the data goes onto the database.

Overview:

This document covers the following steps:
Define two Business Objects 'Project' as variables (one contains data as read from the database; one reflects the entered data),

Add the ActionListener and register it with the 'Store'-button,
Implement the method 'actionPerformed()',

Create the class JS_ErrDB_Project__ActionHandler which will contain the code that is run after button-clicks,
Transfer the values from the GUI-elements to the attributes of the Business Object
,
Call the 'store()' method of the Business Object
,
Verify that the data was stored in the database-table
,
Use the debugger to track down programming errors (if the data does not appear in the database as expected)
,
Verify that entry-errors are detected
.

Preface:

The first part of this step (adding an ActionHandler and reacting to a button-click) is an repetition of an already practised exercise (JS_FC01i - Create and code the class for the 'Task-Frame' to maintain 'Projects' | React to a button-click). Therefore the description is reduced to keywords.

Next, the Business Object 'Project' (JS_ErrDB_Project_BOC) has to be defined as a variable in the Task-Frame.

The third part is to transfer the values entered into the GUI-elements (JTextFields) to the Business Object 'Project'.
The method 'getFromGUI(.....)' is described in detail.

Next, the method 'store()' of the Business Object is called and if there was no semantical mistake made in all derivations of the Business Object, the Data-Base-Access Object and the file with the XML-structure containing parameters for database-access, then the data is stored in the database.

Finally, aside from coding, the database-monitor is used to execute some SQL-commands to see if the table 'Project' was populated with the values entered on the GUI.

Credits:

Too numerous to mention; the idea is available in dozens of versions – I refined a lot of ideas into my system.

Prerequisites:

Define two Business Objects 'Project' as variables:

To define two BO has the following reason:
There is one BO that contains the data as it was read from the database-table when the 'Project' was selected for making a change.
The other BO contains the data as it is represented in the input-fields of the GUI.
This concept makes no sense now – but in a later step, data from the GUI is transferred to the BO after every keystroke (and not after the button 'Store' is clicked like in this execise) and then it can be checked if there was a change made.
With the knowledge about this change, the enabling or disabling of buttons can be controlled.

top.

Add the ActionListener and register it with the 'Store'-button:

A more detailed description about the effects of the Interface 'ActionListener' was already given in JS_FC01i - Create and code the class for the 'Task-Frame' to maintain 'Projects' | React to a button-click. Therefore the description is reduced to keywords.

top.

Implement the method 'actionPerformed'

As mentioned before, the method 'actionPerformed' is called as soon as a button (which has the ActionListener added) is clicked.
To avoid a congestion of the class, all code that handles 'actions' of any kind is put into a own class, the JS_ErrDB_Project__ActionHandler - which will be discussed in the next paragraph.

top.

Create the class JS_ErrDB_Project__ActionHandler

Keying in each SQL-command is pretty boring particularly if the table-definition becomes a little bit more voluminous.
So there are two alternative methods to create a database-table.
Both of them use a text-file with the SQL-commands.

The folder and the name of the file can be chosen freely; I decided for folder 'DB_SCRIPTS' with file 'Create_Tables.txt' under the Eclipse-project JS_FC01

top.

Transfer the values from the GUI-elements to the attributes of the Business Object:

Most of the coding-work was done when the Business Objects were created. Now the fruits of this effort can be harvested as the method implemented in step JS_FC01l - Develop the Client-Side-Class for the Business Object 'Project just have to be called:

top.

Call the 'store()' method of the Business Object:

To store the data to the database, a method of the Client-Side-Derivation of the Business Object is used, too.

top.

Verify that the data was stored in the database-table:

There was a long way from the last visual result till now.
With the coding of all derivations of the Business Object and the Data-Base-Access Object several error might have creeped in.
So, the expected result might not be as expected. In that case, please start with the paragraph Use the debugger to track down programming errors.

top.

Use the debugger to track down programming errors:

As mentioned before, a lot of programming was done after the last step with a visible result.
When I began to implement the process model with the design patterns, I made several small semantical mistakes (forgetting a command, assigning wrong values, etc.) which caused a completely different result than planned.

As it is boring and also error-prone to compare the code with the samples in this tutorial.
The best way to see what is happening in the code and find semantical errors is, to use the debugger.

top.

Verify that entry-errors are detected:

A good practise of software-development is, to verify if the application reacts correct to errors caused by faulty entries of a user.
The final task in this step is, to enter not allowed data and check, if the application responds correct.

The feedback to errors is poor at this stage of development – it is just a technical printout to the 'Console'.
For mature commercial applications there is a much better response shown to the user – this just is not documented yet (October 2006) ;-) .

top.

Next Step: