Programming for a Db2 environment

In general, the coding for a COBOL program will be the same if you want the program to access a Db2® database. However, to retrieve, update, insert, and delete Db2 data and use other Db2 services, you must use SQL statements.

To communicate with Db2, do these steps:

  • Check your cob2.cfg file to ensure that any paths to your specific Db2 version are set up correctly. Use of the cob2_db2 command and corresponding cob2_db2 stanza in the cob2.cfg file are recommended when programming for a Db2 environment. See Modifying the default compiler configuration for more information.
  • Ensure that the PAM package is installed.
  • Code any SQL statements that you need, delimiting them with EXEC SQL and END-EXEC statements. The EXEC SQL CONNECT statement connects to the database when the program is run. To learn more about SQL statements, see Introduction to embedded SQL in the Db2 documentation.
  • Start Db2 if it is not already started before you compile your program.
  • Specify the database that the program uses. The compiler will connect to that database during compilation.
  • If you are connecting to a remote Db2 database, you must configure Db2 to enable authentication through the operating system. See Db2 security model overview for more information.
  • Set the LD_LIBRARY_PATH, NLSPATH, DB2INSTANCE, and SYSLIB environment variables before compiling. Note that the directory LD_LIBRARY_PATH specifies should contain libdb2.so, which is the shared object for the Db2 co-processor, so that the co-processor can be loaded by the compiler and used during the compilation.
    Below is an example:
    export LD_LIBRARY_PATH=/opt/ibm/db2/<Db2_version>/lib32
    export NLSPATH=/opt/ibm/db2/<Db2_version>/msg/%L/%N
    export DB2INSTANCE=db2in111
    export SYSLIB=/opt/ibm/db2/<Db2_version>/include/cobol_a
    Note: The copybook files under the cobol_a directory are included only in Db2 11.5.6. If you are using an earlier Db2 version, you need to contact COBOL.Linux.Trial@ca.ibm.com to get these files.
  • Compile with the SQL compiler option.
  • Compile with the NODYNAM compiler option.
    Note: The NODYNAM compiler option is required for programs that contain EXEC CICS® or EXEC SQL statements.

    If EXEC SQL statements are used in COBOL programs that are loaded by a COBOL dynamic call, then one or more EXEC SQL statements must be in the main program. In a Db2 application, the main program must do the initial load of the Db2 libraries before any dynamically called program attempts to use those libraries.

  • Use the -L and -l options when linking. Below is an example:
    -L/opt/ibm/db2/<Db2_version>/lib32 -ldb2
    Note: You might experience an undefined reference to sqlgstrt, sqlgaloc, sqlgstlv, sqlgcall, sqlgstop, and other symbols such as undefined reference to `SQLGSTRT'. To resolve the issue, specify the Db2 libraries using the -L and -l options when linking your program. These options must appear after the COBOL source files specified on the command line. Here is an example:
    filea.cbl -L/opt/ibm/db2/<Db2_version>/lib32 -ldb2

Related concepts  
Db2 coprocessor