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 : Page 2

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.

Seeding Your Database with Some Sample Data
The Data Definition view can be used to populate records into the database table you just created. Right-click on the Statements folder and select New>Insert Statement (see Figure 6). You will be asked to specify a name for your statement. Call your statement 'PopulateTable'.

Figure 6. Insert Statement: Use the Data Definition view in Application Developer to tell the application how to populate data from records.
Figure 7. Add Table: Make sure to select the proper table name for your application from the dialog that appears after hitting the 'Add Table' button; then click OK.

In the view that appears next, right-click in the Tables area and select Add Table (see Figure 7). Make sure the table name of MYSCHEMA.MYTABLE is selected in the "Add Table" window that follows and click OK.

Next, in the Tables area, click the SSN, LASTNAME, and FIRSTNAME columns. Go ahead and use the wizard to specify values for the columns. I specified 111111111 for the SSN, 'Washington' for the LASTNAME, and 'George' for the FIRSTNAME column (see Figure 8).

Figure 8. Populate Table: Create a record with SSN, last name, and first name that will be used to populate your database.
Figure 9. Success: When your INSERT statements executes properly, Application Developer lets you know by displaying a Success message in the DB Output view.

You can view your associated DDL for your insert query in the SQL source area. You should see:

Press Ctrl+S for the statement you just created to be saved. To issue the SQL statement against the database, right-click on the on the PopulateTable statement under the Statements folder in the Data Definition view and select the Execute context option.

If the INSERT statement executes properly, you should see a successful message shown in the DB Output view (see Figure 9).

Go ahead and issue another INSERT statement against the database as you just did for the following values:

111111112 John Adams

Accessing Your Cloudscape Database Via JDBC
Now that you have created your Cloudscape database, a housing schema, and a table—seeded with sample data—within your database, you're ready to finish off by writing a simple Java application that accesses that database. However, before doing so, you need to import the requisite JDBC driver for accessing the Cloudscape database.

In the Java perspective, right-click on the TestProject project and choose Properties from the context menu. In the Properties window, choose 'Java Build Path' in the left-hand pane and in the right-hand pane choose the Libraries tab and then the Add External JARs button.

Navigate to your Rational Application Developer installation directory and under it navigate to the runtimes\base_v6\cloudscape\lib directory. In Windows the default installation directory would be:

C:\Program Files\IBM\Rational\SDP\6.0\runtimes\base_v6\cloudscape\lib
After navigating to the lib directory, select the file: db2j.jar and click Open. After adding the file db2j.jar to your build path, choose the OK button (see Figure 10).

Figure 10. Build: In preparation for testing your application, add the proper JAR file to your Java build path.
Figure 11. Working App: Testing the completed sample application outputs the data to the Console View.

At this point you are ready to create a new Java class. Creating classes in Rational Application Developer is outside the scope of this article. Instead I'll concentrate on analyzing an existing class that accesses Cloudscape via JDBC. The class named TestClass is shown below:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestClass {

  public static void main(String[] args) {

    try {
      System.out.println("Testing Cloudscape DB connection");
      String url = "jdbc:db2j:c://temp//mytestdb;create=false";
      Connection cn = 
      Statement st = 
      ResultSet rs = 
        st.executeQuery("SELECT * FROM MYSCHEMA.MYTABLE");
      System.out.println("SSN: LASTNAME, FIRSTNAME");
      while (rs.next())
        System.out.println(rs.getInt("SSN") + ": " 
        + rs.getString("LASTNAME") + ", "
        + rs.getString("FIRSTNAME"));
      } catch (ClassNotFoundException e) {
    } catch (SQLException e) {

Most of the code above should be quite familiar to you. Of particular importance and relevance to the Cloudscape database connection is the loading of the database driver via the line below. (Notice the use of the driver: com.ibm.db2j.jdbc.DB2jDriver, which is particular to connecting to Cloudscape.)

Next I need to specify the URL for connecting to my database:

String url = "jdbc:db2j:c://temp//mytestdb;create=false";
The URL takes the form:

In this URL, 'database path' refers to the path you specified earlier when you created your database connection (in my case c://temp//mytestdb).

To obtain a connection to your database use the call:

Connection cn = 
In the syntax above I am using the java.sql.DriverManager object's getConnection method, which takes in the database URL I established earlier as my first argument followed by the user name and password. Rather than specify a user name and password (they are not required) to connect to the embedded Cloudscape database, I've provided my method with the value "" for both the user name and password.

Finally, I issue a SELECT statement against the database, namely:

This SELECT statement will retrieve all of the data from the MYSCHEMA.MYTABLE table.

At the end of the Java class, I cycle through the java.sql.ResultSet object using a while loop and print out my table contents:

while (rs.next())
     System.out.println(rs.getInt("SSN") + ": " 
     + rs.getString("LASTNAME") + ", "
     + rs.getString("FIRSTNAME"));
Finally you're ready to give the application a test run. The output of the test run is shown in the Console View in Figure 11.

In this article, you learned how you can leverage the Cloudscape database (which ships with IBM Rational Application Developer Version 6.0) to act as a small footprint database that can test your JDBC code.

Kulvir Singh Bhogal works as an IBM consultant, devising and implementing J2EE-centric solutions at customer sites across the nation.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date