Rational Developer for System z, Version 7.6

Connecting to a DB2 database using COBOL

Before attempting to connect to a DB2® database, you must first configure a database connection (refer to Configuring a database connection) and then configure your workbench to use that database connection with the Database Connection wizard (refer to Setting up the connection to a DB2 UDB zSeries database for an example of how to connect to an OS/390® DB2 database).
To connect to a DB2 database using COBOL, do the following:
  1. Create a local COBOL project in Rational® Developer for System z®.
  2. Link the DB2 library to your project by opening the Local Link Optionsproperty page.
  3. Type /de db2api.lib in the Link Options field.
  4. Click Local COBOL Build Options and provide the following information:
    SYSLIB
    installation_directory\INCLUDE\COBOL_A
    Note: Replace installation_directory with your DB2 installation directory.
  5. Select Source contains embedded SQL.
  6. Click Browse and select your DB2 connection.
  7. Optional: Enter additional options to pass onto the SQL preprocessor in the Other SQL options field. Separate multiple SQL options with commas.
  8. Click OK.
    Note: Do not specify DB2-related options in the Compiler Options field. The necessary compiler options and sub-options will automatically be generated for you from your selected DB2 connection and the options you enter in the Other SQL options field.
  9. Insert the following statement before using any SQL statements in the WORKING-STORAGE, LOCAL-STORAGE, or LINKAGE sections of your code:
    EXEC SQL INCLUDE SQLCA END-EXEC;
  10. Delimit SQL statements with EXEC SQL and END-EXEC statements. Here is an example using the DB2 Sample database:
    EXEC SQL UPDATE employee
           SET firstnme = 'Myname'
           WHERE empno = '000010' END-EXEC.
Sample COBOL code to access a DB2 database
      * ---------------------------------------------------
      *   Module Name: COBOLDB2.cbl
      *
      *   Description: Sample COBOL with DB2 program.
      *
      *   Purpose: Performs a Select on the employee table
      *   in the Sample database shipped with DB2.
      *
      *   COMPLILER OPTIONS (be sure to change the USERNAME and PASSWORD):
      *   DATA,EXIT(ADEXIT(FTTDBKW)),sql('database sample user USERNAME using PASSWORD')
      *
      *   SYSLIB:
      *   C:\Program Files\IBM\SQLLIB\INCLUDE\COBOL_A
      *
      *   ILINK OPTIONS:
      *   /de db2api.lib
      *
      * ---------------------------------------------------
       Identification Division.
       Program-ID.  COBOLDB2.

       Data Division.

      *Make sure you have SQLCA included in Working-Storage
       Working-Storage Section.
       EXEC SQL INCLUDE SQLCA END-EXEC.

      *Data structure to store the Firstname of the employee
       01 Program-pass-fields.
          05 Firstnme         Pic x(30).

       Procedure Division.
      *A Connection to the database must be made!
            EXEC SQL CONNECT TO sample END-EXEC.

      *Performs a SQL SELECT to get the firstname of the employee
      *with the employee number of 10.
           EXEC SQL SELECT firstnme INTO :Firstnme
           FROM employee
           WHERE empno = '000010' END-EXEC.

      *Displays the firstname we pulled from the Sample database.
           Display "Firstname"
           Display "========="
           Display Firstnme

           Display " "
      *Displays the status of the SQL statements
           Display SQLCA

           Goback.

Terms of use | Feedback

This information center is powered by Eclipse technology. (http://www.eclipse.org)