DB2 Programming Interface




CS185c

Chris Pollett

Apr 15, 2010

Outline

Introduction

Interface Mechanisms

There are four basic interface mechanisms that can be used with DB2:

We will next look at each of these mechanisms in turn.

Language Extension

How to prepare Embedded SQL

steps to prepare embedded SQL

Static SQL -- COBOL Example

...
DATA DIVISION. 
...
01 MGR-NUM PIC 9(2). 
01 INTDEPT PIC X(6). 
EXEC SQL INCLUDE SQLCA END-EXEC.
...

PROCEDURE DIVISION. ...
EXEC SQL
UPDATE DEPT SET MGRNO = :MGR-NUM WHERE DEPTNO =:INTDEPT
END-EXEC.
...

Static SQL -- C Example

...
EXEC SQL INCLUDE SQLCA;
... 
EXEC SQL BEGIN DECLARE SECTION; 
short int hvc1, hvc2; 
EXEC SQL END DECLARE SECTION; 
... 
hvc2 = input_variable; 

EXEC SQL SELECT MAX(C1) INTO :hvc1 FROM T1 WHERE C2>:hvc2;
...

Execution of a Static SQL

How packages are in the execution of static SQL

SQLJ

SQLJ Preparation

The steps involved in SQL preparation

Connect to a Database from SQLJ

Embedded Dynamic SQL

A Dynamic SQL Example

DATA DIVISION.
...
01 STMT 49 STMT-LENGTH PIC S9(4) COMP VALUE +255. 
49 STMT-TEXT	PIC X(255). 
01 VDEPNO	PIC X(6). 
...
PROCEDURE DIVISION.
...
Move "DELETE FROM EMP WHERE DEPTNO = (CAST ? AS PICX(6))" TO STMT 
... 
EXEC SQL PREPARE S1 FROM :STMT END-EXEC
...
MOVE 'A00' TO VDEPNO 
... 
EXEC SQL EXECUTE S1 USING :VDEPNO END-EXEC .
...

Dynamic SQL Statements

There are four forms of statements for embedded dynamic SQL: PREPARE, DESCRIBE, EXECUTE, and EXECUTE IMMEDIATE. In addition, we have the auxiliary forms: "OPEN", "FETCH" and "CLOSE" statements to support the cursor select statement.

SQLDA

Dynamic SQL Execution

APIs - ODBC and JDBC

Open Database Connectivity (ODBC)

Advantages of Using ODBC

ODBC Process Model

Processing flow in an ODBC transaction

More on the ODBC Processing Model