Welcome!

IBM Cloud Authors: Zakia Bouachraoui, Elizabeth White, Yeshim Deniz, Pat Romanski, Liz McMillan

Related Topics: IBM Cloud

IBM Cloud: Article

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.

More Stories By 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.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it results in an overall increase in consumption. Writing on the increased use of coal as a result of technological improvements, 19th-century economist William Stanley Jevons found that these improvements led to the development of new ways to utilize coal. In his session at 19th Cloud Expo, Mark Thiele, Chief Strategy Officer for Apcera, compared the Jevons Paradox to modern-day enterprise IT, examin...
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO and DXWorldEXPO are two of the most important technology events of the year. Since its launch over eight years ago, @CloudEXPO and DXWorldEXPO have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, we provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading...
DSR is a supplier of project management, consultancy services and IT solutions that increase effectiveness of a company's operations in the production sector. The company combines in-depth knowledge of international companies with expert knowledge utilising IT tools that support manufacturing and distribution processes. DSR ensures optimization and integration of internal processes which is necessary for companies to grow rapidly. The rapid growth is possible thanks, to specialized services an...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...