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:
-
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 Linux®, UNIX, and Windows drivers
- You can use the client drivers to connect to Db2 for z/OS from application programming languages such
as Node.js, Perl, Python, Ruby on Rails, PHP, and others.
- Optional:
Declare the tables and views that you
use.
You can use DCLGEN to generate these declarations.
-
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.
-
Define at least one SQL descriptor area
(SQLDA).
-
Declare any of the following data items for passing data between Db2 and a host language:
-
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.
-
Check the execution of the SQL
statements.
-
Handle any SQL error codes.