Welcome!

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

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
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...
AI and machine learning disruption for Enterprises started happening in the areas such as IT operations management (ITOPs) and Cloud management and SaaS apps. In 2019 CIOs will see disruptive solutions for Cloud & Devops, AI/ML driven IT Ops and Cloud Ops. Customers want AI-driven multi-cloud operations for monitoring, detection, prevention of disruptions. Disruptions cause revenue loss, unhappy users, impacts brand reputation etc.
Atmosera delivers modern cloud services that maximize the advantages of cloud-based infrastructures. Offering private, hybrid, and public cloud solutions, Atmosera works closely with customers to engineer, deploy, and operate cloud architectures with advanced services that deliver strategic business outcomes. Atmosera's expertise simplifies the process of cloud transformation and our 20+ years of experience managing complex IT environments provides our customers with the confidence and trust tha...
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...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
As you know, enterprise IT conversation over the past year have often centered upon the open-source Kubernetes container orchestration system. In fact, Kubernetes has emerged as the key technology -- and even primary platform -- of cloud migrations for a wide variety of organizations. Kubernetes is critical to forward-looking enterprises that continue to push their IT infrastructures toward maximum functionality, scalability, and flexibility. As they do so, IT professionals are also embr...
The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get tailored market studies; and more.
As you know, enterprise IT conversation over the past year have often centered upon the open-source Kubernetes container orchestration system. In fact, Kubernetes has emerged as the key technology -- and even primary platform -- of cloud migrations for a wide variety of organizations. Kubernetes is critical to forward-looking enterprises that continue to push their IT infrastructures toward maximum functionality, scalability, and flexibility.
Today's workforce is trading their cubicles and corporate desktops in favor of an any-location, any-device work style. And as digital natives make up more and more of the modern workforce, the appetite for user-friendly, cloud-based services grows. The center of work is shifting to the user and to the cloud. But managing a proliferation of SaaS, web, and mobile apps running on any number of clouds and devices is unwieldy and increases security risks. Steve Wilson, Citrix Vice President of Cloud,...
When Enterprises started adopting Hadoop-based Big Data environments over the last ten years, they were mainly on-premise deployments. Organizations would spin up and manage large Hadoop clusters, where they would funnel exabytes or petabytes of unstructured data.However, over the last few years the economics of maintaining this enormous infrastructure compared with the elastic scalability of viable cloud options has changed this equation. The growth of cloud storage, cloud-managed big data e...