Overview of programming applications that access DB2 for z/OS data

Applications that interact with DB2® must first connect to DB2. They can then read, add, or modify data or manipulate DB2 objects.

About this task

A query is an SQL statement that returns data from a DB2 database. Your program can use several methods to communicate SQL statements to DB2 for z/OS®. After processing the statement, DB2 issues a return code, which your program can test to determine the result of the operation.

Procedure

To include DB2 for z/OS queries in an application program:

  1. Choose one of the following methods for communicating with DB2:
    Static SQL
    The source form of a static SQL statement is embedded within an application program written in a host language. The statement is prepared before the program is executed and the operational form of the statement persists beyond the execution of the program.
    Embedded dynamic SQL
    Dynamic SQL is prepared and executed while the program is running.
    Open Database Connectivity (ODBC)
    You access data through ODBC function calls in your application. You execute SQL statements by passing them to DB2 through a ODBC function call. ODBC eliminates the need for precompiling and binding your application and increases the portability of your application by using the ODBC interface.
    JDBC application support
    If you are writing your applications in Java, you can use JDBC application support to access DB2. JDBC is similar to ODBC but is designed specifically for use with Java.
    SQLJ application support
    You also can use SQLJ application support to access DB2. SQLJ is designed to simplify the coding of DB2 calls for Java applications.
    DB2 for z/OS with DB2 for Linux, UNIX, and Windows drivers
    You can use the drivers to connect to DB2 from application programming languages such as Node.js, Perl, Python, Ruby on Rails, PHP, and others.
  2. Optional: Declare the tables and views that you use. You can use DCLGEN to generate these declarations.
  3. Define the items that your program can use to check whether an SQL statement executed successfully. You can either define an SQL communications area (SQLCA) or declare SQLSTATE and SQLCODE host variables.
  4. Define at least one SQL descriptor area (SQLDA).
  5. Declare any of the following data items for passing data between DB2 and a host language: Ensure that you use the appropriate data types. For details, see Compatibility of SQL and language data types
  6. Code SQL statements to access DB2 data. Make sure to delimit the statements correctly for the specific programming language. For more information about coding SQL statements in host languages, see the language-specific information for your programming language:

    Consider using cursors to select a set of rows and then process the set either one row at a time or one rowset at a time.

  7. Check the execution of the SQL statements.
  8. Handle any SQL error codes.

What to do next