Models for Database Applications

The model contains the state information that drives the UI display. It is therefore the starting point for understanding how to build a Swing application. The banking application needs to provide a model that organizes the banking business objects for the appropriate UI component models. Before we dive into the complexities of three-tier UI component modeling, however, I want to step back and look at a simpler two-tier example. This two-tier example presents the basic concepts we will see later in a more flexible three-tier model without the need to worry about distributed computing issues.

A Two-Tier Model

The simplest example of a two-tier database application is one that queries a database and stores the results in a table. In Swing, the JTable UI delegate and TableModel model represent the table component. The table model captures a database result set and tells the table view what the column and value names are for each row. The table view then provides a nice tabular display of the data.

Swing makes it possible for you to ignore all of the display issues. Your concern is handling events and providing accurate state information in the model. It is surprising just how easy it is to construct such a model for database access. You need only to extend the AbstractTableModel class provided in the Swing API and delegate to the RowSet class covered in Chapter 5. The result is the class in Example 10.1.

Example 10-1. A RowSet Model for Constructing ...

Get Database Programming with JDBC & Java, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.