Contents


Establishing an IBM DB2 database connection in IBM Rational Functional Tester

Only 3 easy steps, and you're finished

Comments

Prerequisite

Installation of IBM Rational Functional Tester Version 7.0 (see Related topics 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
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.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

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