IBM Cloud Authors: Yeshim Deniz, Elizabeth White, Zakia Bouachraoui, 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() {
DAOException(String 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;
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.

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
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...
Intel is an American multinational corporation and technology company headquartered in Santa Clara, California, in the Silicon Valley. It is the world's second largest and second highest valued semiconductor chip maker based on revenue after being overtaken by Samsung, and is the inventor of the x86 series of microprocessors, the processors found in most personal computers (PCs). Intel supplies processors for computer system manufacturers such as Apple, Lenovo, HP, and Dell. Intel also manufactu...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
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...
Apptio fuels digital business transformation. Technology leaders use Apptio's machine learning to analyze and plan their technology spend so they can invest in products that increase the speed of business and deliver innovation. With Apptio, they translate raw costs, utilization, and billing data into business-centric views that help their organization optimize spending, plan strategically, and drive digital strategy that funds growth of the business. Technology leaders can gather instant recomm...
OpsRamp is an enterprise IT operation platform provided by US-based OpsRamp, Inc. It provides SaaS services through support for increasingly complex cloud and hybrid computing environments from system operation to service management. The OpsRamp platform is a SaaS-based, multi-tenant solution that enables enterprise IT organizations and cloud service providers like JBS the flexibility and control they need to manage and monitor today's hybrid, multi-cloud infrastructure, applications, and wor...
The Master of Science in Artificial Intelligence (MSAI) provides a comprehensive framework of theory and practice in the emerging field of AI. The program delivers the foundational knowledge needed to explore both key contextual areas and complex technical applications of AI systems. Curriculum incorporates elements of data science, robotics, and machine learning-enabling you to pursue a holistic and interdisciplinary course of study while preparing for a position in AI research, operations, ...
After years of investments and acquisitions, CloudBlue was created with the goal of building the world's only hyperscale digital platform with an increasingly infinite ecosystem and proven go-to-market services. The result? An unmatched platform that helps customers streamline cloud operations, save time and money, and revolutionize their businesses overnight. Today, the platform operates in more than 45 countries and powers more than 200 of the world's largest cloud marketplaces, managing mo...
Trend Micro Incorporated, a global leader in cybersecurity solutions, helps to make the world safe for exchanging digital information. Our innovative solutions for consumers, businesses, and governments provide layered security for data centers, cloud workloads, networks, and endpoints. All our products work together to seamlessly share threat intelligence and provide a connected threat defense with centralized visibility and investigation, enabling better, faster protection. With more than 6,00...
Tapping into blockchain revolution early enough translates into a substantial business competitiveness advantage. Codete comprehensively develops custom, blockchain-based business solutions, founded on the most advanced cryptographic innovations, and striking a balance point between complexity of the technologies used in quickly-changing stack building, business impact, and cost-effectiveness. Codete researches and provides business consultancy in the field of single most thrilling innovative te...