|
Database Programming
Motivation
Embedded static SQL
Embedded dynamic SQL
ODBC/CLI
JDBC
Motivation
Database Programming is a quite challenging task. Most
users do not even know that they are using a database behind the scence. End
users who type in interactive SQL are known as casual users [Patrick
O'Neal: "Database, Principles, Programming, Performance"]. However, this way is not acceptable
for applictions that are using a database. As a matter of fact there
are different options to access the database within an application!
Embedded static SQL
"In the static SQL approach, the application developer must know exactly what SQL statements are needed
and must write these SQL statements directly into the application program. The program is then proceeded by the
precompiler, which converts each SQL statement into an optimzed access plan and stores the plan in the database."
[Don Champerlin: DB2 Universal Database]
You can write your programs with different languages such as C, C++, Fortran and Cobol.
The sample source code can be seen via a C-programm
embedded static SQL!
Embedded dynamic SQL
In static SQL we have seen that the developer needs to know the SQL statements in advance. However, this not always
possible. The program has to generate the SQL statements at run time. For this purpose, the approach of dynamic SQL has been imtroduced.
The sample source code can be seen via a C-programm
embedded dynamicSQL!
ODBC/CLI
The Call Level Interface (CLI) of DB2 has basically the same syntax of an ODBC program. However, sicne you use some header files
of DB2 it provides some optimations. It implements currently the ODBC 3.0, Level 1.The Call Level Interface is developed for C. The
sample code (CLI) shows the use of it! The CLI has several advantages over Embedded Dynamiy SQL.
- CLI does not require a precompiler. By linking your program to the CLI library, you have access to all SQL facilities.
- CLI programs (unlike Embedded Dynamic SQL) can connect to the same database multiple time and can indepently commit transactions in each database connection-
- Unlike Embedded Dynamic SQL, CLI supports scrollable sursors, which can be advanced forward, backward, or to an absolute position within a result set.
JDBC
The Java Database Connectivity (JDBC) is a dynamic SQL interface for the Java language. A JDBC application is much like any other database application
that is written in a host programming language. As usual, the application runs on a client machine and the database resides on aserver machine.
An applet is a Java program that can be executed by a web browser during the process of displaying a page from the World Wide Web (www). The applet, in turn, can use the
facilities of JDBC to connect to a database, retrieve data, and display the data as part of the web page.
There are two sample programs. One shows how to use JDBC within an
Applet and another one form an
application! The main difference is the driver. Whereas the applet needs to downlaod all
required classes, the application does not have to do it necessarily!
Note:
All sample code has been tested with IBM DB2
Version 7.2. In order to use the source you need to modify the source code
according to the documentation of your database management system!
|