Skip to main content

Establishing an IBM DB2 database connection in IBM Rational Functional Tester

Only 3 easy steps, and you're finished

Fariz Saracevic (fariz@us.ibm.com), Senior IT Architect, IBM
Fariz Saracevic is a Senior IT Architect for IBM Rational Software Services. He has a background in quality assurance, and his primary focus is software test automation. Fariz has worked with numerous software test teams in consulting, mentoring, and training capacities. He holds a master's degree in information technology from Virginia Tech.
Mamatha Narasappa (mamatha.narasappa@gartner.com), Senior Test Engineer, Gartner Inc
Mamatha Narasappa is working as a Senior Test Engineer in Gartner Inc. She has expertise in Testing, Automation & Release activities. She has strong background of Software Development Life Cycle and Quality Assurance Life Cycle. She has worked numerous test teams and on different domains.

Summary:  Learn how to establish an IBM DB2 database connection using IBM Rational Functional Tester Java scripting. All you need to do is to follow these three simple steps and use the sample code provided.

Date:  20 Nov 2007
Level:  Intermediate
Activity:  1282 views
Comments:  

Prerequisite

Installation of IBM Rational Functional Tester Version 7.0 (see Resources for trial download).

You can establish an IBM® DB2® database connection in IBM® Rational® Functional Tester simply by following three easy steps:

  1. Install the JDBC driver.
  2. Create a DB2 connection class.
  3. Add code to the scripts.

Note:
This article includes sample code that you can use for each step.

Install the Java Database Connectivity driver

This is the first step toward connecting to the database. You will need to install a database driver for the database that you will be using.

If you have a legacy DB2 database, load the Type 2 Java Database Connectivity (JDBC) driver. The legacy call-level interface (CLI) JDBC driver is provided in the db2java.zip file, which is in the default DB2 installation directory, C:\Program Files\IBM\SQLLIB\java.

Note:
If you use Universal JDBC, load the Type 4 JDBC driver. The driver is provided in the db2jcc.jar file and its corresponding license Java Archive (JAR) file, which is in the default DB2 installation directory, C:\Program Files\IBM\SQLLIB\java.

Load the db2java.zip file to the project (also see Figure 1):

  1. Select the Project menu, and then select Properties.
  2. Select the Java Build Path property.
  3. Select the Libraries tab, and click Add External JARs.
  4. Browse to the .zip file location and add it.

Figure 1. Adding the driver to your project
Adding the driver to your project screen capture

Create the DB2 connection class

After you have installed the required JDBC driver, you need to create the DB2 connection class, which will contain the DB2 connection code and methods to communicate with the database. For better consistency, it’s a good practice to isolate the code that actually interacts with the database, and then just pass these database statements to the methods in the code.

  1. Create a new class for database connection by creating a new script.
  2. For the newly created script, add the code shown in Listing 1, modifying the database name, username, and password.

Listing 1. The DB2 connection class
                
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import javax.sql.*;
import COM.ibm.db2.jdbc.app.DB2Driver;

public class DB2Connection {
	/**
	 * Script Name   : <b> DB2Connection</b>
	 * Description   : Functional Test Script
	 * Original Host : WinNT Version 5.1  Build 2600 (S)
	 * 
	 * @author Admin
	 */
	
	public Connection connection = null;
	public ResultSet resultset = null;
      public Statement statement = null;
	
void connect() throws Exception 
	{
		Driver driver = new COM.ibm.db2.jdbc.app.DB2Driver();
            DriverManager.registerDriver(driver);
		System.out.println("Driver Loaded Successfully ...");
	connection = DriverManager.getConnection("jdbc:db2:<database name>", 
"username", "password");					
		if (connection == null)
			{
				System.out.println("connection failed");
			}
		connection.setAutoCommit(true);
		System.out.println("Successfully Connected to DB2...");
	statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
ResultSet.CONCUR_UPDATABLE);
	}
	
public ResultSet query(String arg) throws SQLException 
	{
		try 
		{	
statement = connection.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);	         
	resultset = statement.executeQuery( Select * from <TABLE_NAME> where <CONDITION>); 
    		}catch (Exception ioe) 
				{
					System.out.println("Query failed: " + arg);
					ioe.printStackTrace();
				}
			return resultset;
	}


public void close () throws Exception 
	{
		if (connection != null)
			{
				try
					{
						connection.commit();
						connection.close();
					}catch (Exception e)
						{
	System.out.println("Problem in closing DB2 connection: " + e.getMessage());
						}
					connection = null;
			}
	}
}



Add the DB2 connection class to your scripts

When you have created the DB2 connection class and modified the specific database connection, you need to add the code in Listing 2 to your scripts after this line:
public void testMain (Object [] args)


Listing 2. Code to add the DB2 connection class to your scripts
                
DB2Connection db2c = new DB2Connection();
Resultset results;

try	
{
db2c.connect();
results = connection.query(Select * from <TABLE_NAME> where <CONDITION>);

}catch(Exception e)	
{
	e.printStackTrace();
}


When you have completed these steps in place, your scripts can establish a database connection and retrieve the data.


Resources

Learn

  • Visit the Rational Functional Tester area on IBM® developerWorks® for technical resources including articles, forums, trial software information, and more.

  • Get an Introduction to IBM Rational Functional Tester 7.0. The IBM Rational Functional Tester tool automates testing Java, .NET, and Web-based applications. Starting with Version 7.0, it includes support extensions for both Siebel and SAP, plus integration with IBM Rational ClearQuest, support for the Eclipse Test and Performance Tools Platform (TPTP) logs, and support for testing HTML applications with Mozilla Firefox. This article explains these new features and capabilities (developerWorks, December 2006).

  • Visit the DB2 area on developerWorks to learn more about DB2.

  • Visit the Rational software area on developerWorks for technical resources and best practices for Rational Software Delivery Platform products.

  • Check out the Rational Edge for articles on the concepts behind effective software development.

  • Subscribe to the IBM developerWorks newsletter, a weekly update on the best of developerWorks tutorials, articles, downloads, community activities, webcasts and events.

  • Browse the technology bookstore for books on these and other technical topics.

Get products and technologies

Discuss

About the authors

Fariz Saracevic is a Senior IT Architect for IBM Rational Software Services. He has a background in quality assurance, and his primary focus is software test automation. Fariz has worked with numerous software test teams in consulting, mentoring, and training capacities. He holds a master's degree in information technology from Virginia Tech.

Mamatha Narasappa is working as a Senior Test Engineer in Gartner Inc. She has expertise in Testing, Automation & Release activities. She has strong background of Software Development Life Cycle and Quality Assurance Life Cycle. She has worked numerous test teams and on different domains.

Comments



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational, Information Management
ArticleID=269669
ArticleTitle=Establishing an IBM DB2 database connection in IBM Rational Functional Tester
publish-date=11202007
author1-email=fariz@us.ibm.com
author1-email-cc=rjbence@us.ibm.com
author2-email=mamatha.narasappa@gartner.com
author2-email-cc=rjbence@us.ibm.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers