IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
      
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks > Rational >
developerWorks
Establishing a database connection in IBM Rational Functional Tester scripts
e-mail it!
Contents:
Installing the JDBC driver
Creating the JDBC connection class
Using the JDBC connection class in your scripts
Resources
About the author
Rate this article
Subscriptions:
dW newsletters
The Rational Edge

Level: Intermediate

Fariz Saracevic (fariz@us.ibm.com)
Senior IT Specialist, IBM
08 Mar 2005

It only takes three simple steps to establish a database connection in IBM Rational Functional Tester scripts. Read about them and use the code provided within this step-by-step guide.

To establish a database connection in scripts generated by IBM® Rational® Functional Tester, you need to take three simple steps. First you install the JDBC driver, then you create a JDBC connection class, and finally you add code to your script to use this class. This article walks you through these steps.

Editor's note: This article applies to Version 2003.06.13 and earlier releases of IBM Rational Functional Tester.

Installing the JDBC driver
The first thing you need to do is to copy the appropriate JDBC driver for your specific DBMS onto the machine where your project is located. No special configuration is needed. For example, to install the JDBC driver for an Oracle database, select from the list of JDBC Driver Downloads on the Oracle Web site.

Once you've downloaded the correct driver, you need to add it to your project. Right-click the project and select Properties. Click the Libraries tab and then click Add External JARs, as demonstrated in Figure 1. Then select the JAR containing the driver.

Figure 1. Adding the driver to your project
The Libraries tab of the Properties window, showing the Add External JARs button that you click

Creating the JDBC connection class
Once you've installed the JDBC driver, you need to create the JDBC connection class, which will contain the JDBC connection code and methods to talk with the database.

First select File > New > Class and specify the class name (in our example in Figure 2, OracleConnection). Then accept other default settings and click Finish. This will open a new window.

Figure 2. Specifying the name for our new connection class
The Java Class tab of the New dialog, with a field for specifying the class name

For the newly created class, add the code shown in Listing 1, modifying the host name, service, user name, and password as appropriate for your database connection.

Listing 1. The JDBC 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;

/**
 * @author Admin
 *
 * To change this generated comment, edit the template variable
 * "typecomment": Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

public class OracleConnection
{

   public static void main(String[] args) throws SQLException
   {

      new OracleConnection();
   }

   OracleConnection() throws SQLException
   {
      connect();
      // this.query();

   }

   Connection connection;

   void connect() throws SQLException
   {

      Driver driver = new oracle.jdbc.OracleDriver();
      DriverManager.registerDriver(driver);
      connection = DriverManager.getConnection("jdbc:oracle:thin:
         @<hostname>:1521:<service>", "username", "password");
      connection.setAutoCommit(true);

   }

   void query() throws SQLException
   {

      ResultSet resultset = null;
      Statement statement = null;

      try
      {

         statement = connection.createStatement
            (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         resultset = statement.executeQuery("Select * from TAX_RETURN where 
            rownum<=10 order by  1"); 

         ResultSetMetaData rsmd = resultset.getMetaData();
         int columns = rsmd.getColumnCount();

         while (resultset.next())
         {

            for (int i = 0; i < columns; i++)
            {
               System.out.print(resultset.getObject(i + 1) + " ");
            } // for

            System.out.println();
         } // while

      }
      finally
      {
         if (resultset != null)
            try
            {
               resultset.close();
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         if (statement != null)
            try
            {
               statement.close();
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         // if (connection != null) ...return to pool...
      }

   } //executeQuery

   ResultSet query(String arg) throws SQLException
   {

      ResultSet resultset = null;
      Statement statement = null;

      try
      {

         statement = connection.createStatement
            (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         resultset = statement.executeQuery(arg);

         ResultSetMetaData rsmd = resultset.getMetaData();
         int columns = rsmd.getColumnCount();

         while (resultset.next())
         {

            for (int i = 0; i < columns; i++)
            {
               System.out.print(resultset.getObject(i + 1) + " ");
            } // for

            System.out.println();
         } // while

      }
      finally
      {
         if (resultset != null)
            try
            {
               resultset.close();
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         if (statement != null)
            try
            {
               statement.close();
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         // if (connection != null) ...return to pool...
      }

      return resultset;
   } // executeQuery

}

Using the JDBC connection class in your scripts
Once you've created the JDBC 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)

Use the connection.query method to specify the SQL string to use.

Listing 2. Code to add the OracleConnection class to your scripts
OracleConnection connection;
      ResultSet results;

      try
      {
         connection = new OracleConnection();
         results = connection.query("Select * from TAX_RETURN where 
            rownum<=10 order by  1");
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }

That's all there is to it! Your scripts can now establish a database connection.

Resources

About the author
Fariz Saracevic is a senior IT specialist for IBM Software Services Rational. He has a strong background in quality assurance and his primary focus is on 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. He can be reached at fariz@us.ibm.com.


e-mail it!
Rate this article

This content was helpful to me:

Strongly disagree (1)Disagree (2)Neutral (3)Agree (4)Strongly agree (5)

Comments?



developerWorks > Rational >
developerWorks
    About IBM Privacy Contact