VisualAge for Java supports access to relational databases through JDBC. You can access relational data in an applet or application by using the Data Access beans on the Visual Composition Editor beans palette. (If you have VisualAge for Java, Enterprise Edition, you can also access relational data through the Data Access Builder, see When to use Data Access Builder.)
The Data Access beans are a feature of Visual Age for Java, and comprise a Select bean and a DBNavigator bean. To use the Data Access beans, you must first add the Data Access beans feature to Visual Age for Java.
The Select bean is a non-visual bean. Using the Select bean you can query a relational database. You can also insert, update or delete a row in the result set and commit the changes in the database.
When you use a Select bean, you specify properties pertinent to relational database access, for example, when a lock should be acquired for a row in a table. You also specify a query property that contains a connection alias and an SQL specification for the Select bean.
When you create a connection alias or an SQL specification, you identify a database access class to hold the definition. The connection alias and SQL specification for a Select bean can be in the same database access class, but this is not a requirement. In addition, different Select beans can identify the same database connection alias or SQL specification. If different Select beans specify the same connection alias, they share the database connection associated with that connection alias. If one Select bean commits updates to a database, it commits all uncommitted updates made by any Select bean sharing the database connection.
The Select bean provides a set of methods for relational database access. For example it provides an execute method to execute the SQL statement, and an updateRow method to update a row in the database based on data in the current row of the result set. To access relational data using a Select bean, you connect an interface component to the Select bean. For example, you can make an event-to-method connection between the actionPerformed event for a button and the execute method of the Select bean. When the button is selected, the SQL statement associated with the Select bean is executed.
When you execute an SQL statement using a Select bean, it returns a result set. However, the actual number of rows fetched into memory (cache) is controlled by various properties of the Select bean. You can set the value of these properties and thus control how many rows are fetched. The properties determine:
A "subset of the result set" means only the number of rows needed to satisfy a request. For example, if the result set is 100 rows, but the application only displays 10 rows, only 10 rows are fetched into the cache.
You can display data in the result set by making a property-to-property connection between the appropriate source property of the Select bean and an appropriate target property of an interface component such as a text field. If you use the SQL Assist SmartGuide to compose the SQL statement, VisualAge for Java generates two bound properties for each data column in the SQL specification. One property is the data column in its specified data type, another is a String representation of the data column. So, for example, you can make a property-to-property connection between the String representation of a data column in the result set and the text property of a text field.
Many of the Select bean methods are designed to operate on the current row of the result set. When an SQL statement is executed using a Select bean, the first row of the result set is the current row. If a column value of the result set is connected to an interface component such as a text field, the column value in the current row is displayed in the text field.
The Select bean includes methods to change the current row, for example, one method makes the next row in the result set the current row. Furthermore, each column value property for the Select bean is a bound property, so that if the current row is changed, it changes the data displayed in any interface component connected to the bound property.
The Select bean also provides methods that you can use to insert, update, and delete relational data. To perform these operations you must first use a Select bean to retrieve a result set. You then apply the changes to the current row of the result set.
There are various ways to use these methods. For example, one way to implement updates in an application is to make an event-to-script connection between an appropriate interface component, such as a button, and a script. The script accesses the value in another interface component such as a text field, and uses a Select bean method to set the value of the column in the current row of the result set based on the accessed value. The script also uses a Select bean method to update the database with the reset value in the current row.
The DBNavigator bean is a visual bean that is used with a Select bean. The DBNavigator bean provides a set of buttons that execute the SQL statement for the associated Select bean; perform other relational database operations, such as commit updates to the database; and navigate rows in the result set.
The DBNavigator bean is customizable. You can specify which of the buttons in the set you want displayed (however you cannot control the order of buttons in the display). You do this by setting properties in the DBNavigator bean.
To use the DBNavigator bean you create a property-to-property connection between the this property of the Select bean and the model property of the DBNavigator bean. The this property refers to the whole object of the Select bean. The model property specifies that the DBNavigator bean will navigate the associated Select bean.
The Enterprise version of VisualAge for Java includes an Enterprise Access tool called Data Access Builder. It lets you generate beans that perform operations beyond the scope of the data access beans. For example, using Data Access Builder, an applet or application can access rows returned by DB2 stored procedures. You can also use Data Access Builder to generate methods that will perform any action you want on the database contents.
After you develop an application that contains the Data Access beans you can deploy it for use. VisualAge for Java provides several files that are used with these applications at run time. These files, which are in root/eab/runtime20, where root is the VisualAge for Java root directory, contain the class files, in several formats, for the classes in the IBM Data Access Beans project. The classpath should be modified to contain one of these files when an application is deployed.
The files are:
In addition, VisualAge for Java provides two run time directories. These directories contain the same classes that are in the JAR or ZIP file. The directories are automatically added to the classpath when VisualAge for Java is installed. This allows applications that use the Data Access beans to run on the machine in which VisualAge for Java is installed without further modification to the classpath.
The directories are: