 | Level: Intermediate Fariz Saracevic (fariz@us.ibm.com), Senior IT Architect, IBM Mamatha Narasappa (mamatha.narasappa@gartner.com), Senior Test Engineer, Gartner Inc
20 Nov 2007 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.
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:
- Install the JDBC driver.
- Create a DB2 connection class.
- 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):
- Select the Project menu, and then select Properties.
- Select the Java Build Path property.
- Select the Libraries tab, and click Add External JARs.
- Browse to the .zip file location and add it.
Figure 1. Adding the driver to your project
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.
- Create a new class for database connection by creating a new script.
- 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. |
Rate this page
|  |