|
Comments
|
Today's Top SOA Links
Product Review Building Data Access Objects
Building Data Access Objects
May. 23, 2002 12:00 AM
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
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
Let's Begin
The Zen of DAO
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 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 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
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
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
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 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. 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!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||