RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build a Low Overhead Test Bed for Your JDBC Code

Testing your Java applications against a database can be a pretty painless endeavor thanks to the Cloudscape database built in to IBM's Rational Application Developer. For those who have an interest in this platform, this article will show you the ropes of setting up a simple test bed for your JDBC code.

here's a good chance that your enterprise applications interact with a database. Testing your application code against a database is important in making sure that your application works as expected. And while there are certainly plenty of ways to undertake that kind of testing, IBM offers one method that's relatively easy. Unbeknownst to many, Rational Application Developer Version 6, an Eclipse-based IDE from IBM, ships with Cloudscape, a full featured database server with a small footprint.

Cloudscape got a lot of attention recently when IBM opened its source as Apache Derby. Cloudscape is ideal for testing your JDBC code as you'll learn in this article. And while some purists—those who swear by command line, vi editor development—might beg to differ, the majority of developers tend to agree that use of an IDE can catalyze your development effort. If you are already familiar with the Eclipse IDE, then getting up and running with Rational Application Developer won't be that big of a leap. You can download a trial version of Rational Application Developer 6 from http://www-128.ibm.com/developerworks/downloads/r/rad/. For this article, I will assume that you have some basic familiarity with Rational Application Developer. In addition to learning how to test your J2EE applications using Cloudscape, I will show you how to use a number of relational database tools innate in Rational Application Developer.

Creating a Cloudscape Database Sandbox
I'll start off by showing you how to create a simple database and some underlying tables in Cloudscape via Rational Application Developer's Data Perspective. In Application Developer, switch to the Data Perspective by going to Window> Open Perspective > Other… From the subsequent Select Perspective window, choose the "Data Perspective."

From the Data Explorer view, right click and choose 'New Connection' from the context menu. You will be presented with a New Database Connection wizard. Specify a Connection Name. I specified CloudeSpaceCon. Also, specify that you want to "Choose a database manager and JDBC driver" and click Next.

Figure 1. Setting Up: There are quite a few parameters to enter on the New Database Connection screen. You can leave the user name and password fields blank for this sample application.
From the next wizard screen, in the 'Select a database manager' pane, make sure that a database manager type of Cloudscape V5.1 is chosen. Also, make sure that the JDBC driver of "Cloudscape Embedded JDBC driver" is specified. You also need to specify a disk location where your database will be located. I specified a location of c:\temp\mytestdb (see Figure 1). Also, be sure the "Create the database if required" checkbox is checked. This instructs Rational Application Developer to create your database for you.

For an embedded Cloudscape database interaction, you can leave the User ID and Password fields in the 'Specify user information' section blank. You can test your connection to the Cloudscape database by clicking the Test Connection button. Application Developer will create the database on your behalf.

If everything went well in creating your Cloudscape database, you should receive a successful connection message. Go ahead and click Finish on that dialog. When asked to copy the database metadata to the project folder, select No. At this point, you should see an entry for CloudscapeCon in your Database Explorer view.

Creating a Database Table
Before creating your first table, create a Java Project in the Java perspective. To get to the Java Perspective, go to Window> Open Perspective> Java. Call your project TestProject.

Next, in the Data perspective's Data Definition View, right click on the TestProject and from the context menu choose New>Database Definition. In the following Database Definition screen, specify a database name (I called mine TestDB) and specify a database vendor type of Cloudscape 5.1 (see Figure 2). After doing so, click Finish.

Figure 2. Defined: You need to create a definition of the database that is associated with the TestProject.
Figure 3. Name and Number: Create a simple table that includes social security numbers stored as integers.

Expand the TestProject project you just created and under it you should see an icon for TESTDB. Right-click the icon and choose New>Schema Definition. In the subsequent Schema Definition screen, specify a Schema name of MySchema and Click Finish.

Next, go back to the Data Definition view and expand the MySchema item you just created. Under MySchema item, you should see a Tables folder. Right-click on the Tables folder and select New>Table Definition.

In the Table Definition screen that follows, specify a table name of MyTable and click Next. To add columns to the database table, click the 'Add Another' button in the Table Columns screen that follows.

For illustration purposes, I'll create a very simple table that holds social security numbers as well as the last name and first name of the social security number holder. To do this I need to specify a column name of SSN with a column type of INTEGER. Check the Key column checkmark to specify the column will be the primary key (see Figure 3).

Similarly, use the Add Another button to add columns for the LASTNAME and FIRSTNAME. For these columns, specify a column type of VARCHAR with a String length of 25. When you have created all three columns, click Finish.

In order for the creation of the schema and table to take effect, you need to deploy the changes to the database. Go ahead and right-click the TESTDB icon in the Data Definition view. From the context menu, choose the Deploy option.

From the subsequent Deploy wizard, click Next, bringing you to the Data Export Options screen. Choose the "Commit changes only upon success" option as well as the "Generate fully qualified names" option and click Next (see Figure 4).

Figure 4. Deploy: Specify your preferences for how you want the database to handle committing changes and naming.
Figure 5. Use Existing: Tell the application to connect to the database using the existing "CloudscapeCon" connection.

In the next database connection screen, make sure the "Use existing connection" checkbox is checked and that the existing connection of "CloudscapeCon" (i.e., the database connection you established earlier) is selected. Then click Finish (see Figure 5).

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date