Comments
bruce.armstrong wrote: Somebody just said it better than I did, and with more chops to say it: Open Letter to Mark Zuckerberg, Sheryl Sandberg & Facebook Mobile
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..
SYS-CON.TV
Today's Top SOA Links


Building Data Access Objects
Building Data Access Objects

VisualAge for Java supports access to data stores in several ways. One mechanism is a framework of GUI components known as Data Access Beans, which wrap JDBC inside Java GUI components. This allows you to visually create and manipulate database applications using drag-and-drop techniques inside the Visual Composition Editor. This article will explore how VisualAge's Data Access Beans complement the Data Access Object pattern to create powerful, flexible data-access tools

Installing Data Access Beans
In order to use Data Access Beans, they must be added to your workspace. Select the Quick Start -> Add -> Feature item and locate the Data Access Beans and add them. This will add several GUI/visual beans (DBNavigator Bean, CellSelector Bean, RowSelector Bean, etc.) that can be used to build GUI applets and applications. Also added are three nonvisual beans that can be used in non-GUI applets and applications: the Select Bean, Modify Bean, and ProcedureCall Bean. We'll discuss the Select Bean throughout this article.

The Select Bean exposes properties that allow you to configure connection aliases and SQL specifications. By configuring these properties, you can connect to relational databases and access data. The Select Bean allows you to execute queries that return resultsets. You can browse, insert, update, and delete data using these resultsets.

Data Access Beans work with most JDBC drivers; however, the driver classes must first be added to your classpath. The Options window within VisualAge for Java contains a pane for adding files to your classpath. You must locate this pane and add the .jar or .zip file that contains the JDBC driver classes you want to use.

Deploying Data Access Bean Applications
Applications developed using Data Access Beans can be deployed inside any J2EE environment, such as WebSphere. Correct deployment involves modifying the classpath of the target J2EE environment to include two files, ivjdab.jar and ivjdab.zip. These files, residing in the VAJRoot/eab/runtime20 directory, contain the class files defining the Data Access Beans.

Let's Begin
The first thing we need to do is create a new project in VisualAge for Java. From the Visual Age Work bench,choose Selected -> Add -> Project. Name the project something really exciting, like "DAO Tester". Now, right-click on the project name and select Open. Once the project is open, right-click in the Packages pane and select Add -> Package. Give the package any name you like, select Next, and then Finish.

The Zen of DAO
The Data Access Object (DAO) pattern provides an abstraction layer between the business tier and the enterprise information tier. Objects and components residing in the business tier access data sources residing in the enterprise information tier using DAOs. This layer of abstraction hides the details of each specific information-source/data-store implementation. Thus, changes made to each information source/ data store won't affect the business-tier components. The individual DAOs are the only objects that need to change. Our implementation makes use of several standard design patterns, such as the Abstract Factory pattern and the Singleton pattern.

Introducing Our DAO Classes

The first thing we'll do is implement our DAO classes. Each of our interfaces and classes is created by right-clicking on our package name in the Packages pane and selecting Add -> Class or Add -> Interface. Since most of our DAOs will be wrapping exceptions in a specialized exception class, the first thing we'll do is add a generic DAO exception class as follows:

public class DAOException extends RuntimeException
{
public DAOException() {
super();
}
public
DAOException(String s) {
super(s);
}
}

Next, we'll add a generic DAO interface. The DAO interface exposes methods that embody the CRUD (create, retrieve, update, and delete) design pattern. The methods for this interface all take a generic Object parameter. We also add a close method to allow each DAO object to clean up any resources that it might be using.

public interface DAO
{
public void close()
throws DAOException;
public void create(final
Object object)
throws DAOException;
public void delete(final
Object object)
throws DAOException;
public
java.util.Iterator
retrieve(Object object)
throws DAOException; public void update(final
Object object)
throws DAOException;
}

Next, we'll implement our generic DAO factory class as an abstract class because we want to implement only one method in this class and allow the specific factory types to implement the other methods. The DAOFactory class adheres to the Abstract Factory and Singleton design patterns (see Listing 1).

Now we'll add a concrete DAO factory for our database DAO. We extend the DAOFactory class and implement the abstract methods (see Listing 2).

Next, we'll add a concrete DAO class for our database DAO. We create the DAO class and temporarily implement its methods as shown in Listing 3.

Create the Database Application
At this point we'll use a VisualAge utility that creates a GUI application based around the Data Access Beans. Select Project -> Tools -> Create Database Application. The window shown in Figure 1 will appear.

In the Package: box, type the name of your package or select the Browse... button and navigate to your package's name. In the Class name: box, type a name for your database application. For the sake of this article, type "MyDBApp" in this box and select Next. The window shown in Figure 2 will appear.

Select the Edit... button and the window shown in Figure 3 will appear.

Select the New... button and type the name of your package into the Package: box. Type "DBAccess" as the class name in the Class Name: box. Select OK. Make sure your new class name shows in the drop-down list and then select the Add... button (see Figure 4).

Type a name for your new connection alias in the Connection Name: box. Type the URL for your new connection alias in the URL: box. Select the driver for your new connection alias or type it in the JDBC Driver Input: box. Enter the User ID and Password as needed. Select Finish.

Click on the SQL tab. Make sure your Database Access Class is showing in the drop-down list. Select the Add... button. Type in a name for the SQL and select OK. Select the schema you want to work with and select the Add button. Select OK. Select the table you want to work with and select Next. Keep selecting the Next button until you come to the Columns page. Select the columns you want to work with and select Add. Select Finish. Select OK. Select Next. Make sure the Create a navigation bar checkbox and the Create a table or master view checkbox are not selected. Make sure the Create a details view checkbox is not selected. Select Finish. When the Visual Composition window appears, close it. You will see your new database application class in the Types pane. We can now merge our database application with our DAO classes.

All Together Now
For this article, we'll fill in the implementation for the retrieve(Object object) method of the DBDAO class. Listing 4 demonstrates the details for this implementation. Two things to note about this snippet are the use of the Data-ValueObject class, and the use of the com.ibm.ivj.db.uibeans.Select class. We'll discuss the DataValueObject class later. The com.ibm.ivj.db.uibeans.Select class handles most of the work for us. This class is retrieved from our database application class and has most of the database configuration details already set.

Notice that we get the Select object from the database application class, MyDBApp. With this object retrieved, we call the execute method on it to perform our SQL query. We can then access the data using methods on the Select object. If the getSelect() method is declared with private scope, simply navigate to its definition and declare it as public.

The next thing you'll notice is the use of the DataValueObject class. This is a simple data-model class that offers a layer of abstraction between the actual data and any business-tier classes that use the DAO. Our implementation of the DataValueObject class simply defines a DataId property, a JavaClass property, and a DataValue property (see Listing 5).

Miscellaneous Scraps
VisualAge for Java offers a handy little utility called "The Scrapbook." The Scrapbook is a text editor window that allows you to test snippets of Java code. It's very useful for testing, in our case, code fragments generated by the Data Access Beans generation tools.

To open the Scrapbook, select Window -> Scrapbook. You'll be presented with a blank page into which you can type fragments of Java code. You can also copy and paste fragments of code into the blank page or open an existing file into the page. Once you have the code you want to test in the page, select the code to be tested and click the Run button. The results and/or any errors will be displayed.

Summary
VisualAge for Java is a powerful IDE that offers many tools for building Java and J2EE applications. It is tightly integrated with WebSphere. The Enterprise Edition of VisualAge even ships with a built-in WebSphere test environment that allows you to test WebSphere-targeted components and applications before deploying them to a production environment.

VisualAge for Java provides a powerful toolset for building applications that will access enterprise applications and data stores. This toolset includes the Data Access Beans tools and libraries. Combining the Data Access Beans tools with the DAO programming model empowers you with an information-access solution that handles multiple information sources in a generic manner. This solution is powerful, easy to use, and immune to many of the normal maintenance headaches that enterprise developers face.

About J. Jeffrey Hanson
Jeff Hanson has more than 17 years of experience in the industry, including as senior engineer for the Windows OpenDoc port and as lead architect for the Route 66 framework at Novell . He has worked with Java, C/C++, VB, SQL, XML, and other languages. Jeff is currently chief architect for Zareus, Inc.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE