Document 15033198

advertisement
Matakuliah
Tahun
: T0413
: 2009
Development Overview
Pertemuan 11
DB2 Application Development Overview
Many kinds of applications and code to work with DB2:
Server-side development (at the DB2 database
server):
Database objects (Triggers)
Routines (Stored Procedures, UDFs)
-------------------------------------------------------------------------Client-side development (at the client):
May require a DB2 client or driver to be installed
Database applications (in C/C++, .NET, Cobol,
Java, etc)
Bina Nusantara University
Covered
in Part 1
of the course
To be covered
in Part 2
of the course
3
Data Web Services
DB2 Application Development Overview
Server
Client
Development Tools
IBM Data Studio, RDA, RSA, RAD, Visual
Studio, ZendCore for IBM
DB2 Server
Programming Language
DB Application Programming
Interface (API)
Embedded static & dynamic SQL in C/C++,
Cobol, Fortran, REXX, ODBC/CLI,
JDBC/SQLJ, ADO, ADO.NET, OLE DB, PHP,
RoR, etc.
Database
- Triggers
- Stored Procedures
- UDFs
IBM Data Server Client / Driver
Operating System
Operating System
Part 2
of the
course
Part 1
of the
course
Bina Nusantara University
4
Accessing DB2
Command Line
Processor
Administration
Tools
Control Center
Commands
Interactive
SQL
Command Editor
Applications
APIs
Embedded
SQL
Task Center
Journal
Call Level
Interface
Event Analyzer
Health Center
…
JAVA
…
SQL/API
DB2 Engine
Bina Nusantara University
5
Application Development Freedom
•
•
•
•
•
•
•
•
•
•
•
•
•
Ruby on Rails
C/C++ (ODBC and Static SQL)
JDBC and SQLJ
Borland
Python
PHP
Perl
.NET languages
OLE-DB
ADO
Web Services
SQL
MS Office: Excel, Access, Word
Bina Nusantara University
6
Embedded SQL
Database
#include <stdio.h>
hello.sqc
#include <stdlib.h>
Package
..
hello.bnd
Only
int main(int argc, char**
Executable
bind hello.bnd
SQL, no
argv)
SQL with
C code
{
access path
EXEC SQL BEGIN
information
DECLARE SECTION;
char dbname[15] ;
precompile hello.sqc bindfile
hello.exe needs the
EXEC SQL END
right package to run
DECLARE SECTION;
successfully
...
/* connect to a database
hello.exe
hello.o
hello.c
Only C
*/
code,
EXEC SQL CONNECT
compile
link
Executabl
Object File
no
TO :dbname USER :raul
e
embedded
USING :psw;
File
SQL
if (SQLCODE != 0) {
printf ("\n *** Error
***\n");
Bina Nusantara University
7
Static vs. Dynamic SQL
•
Static SQL
– The SQL statement structure is fully known at precompile time.
• Example:
SELECT lastname, firstname FROM employee
• The names for the columns (lastname, firstname) and tables
(employee) referenced in a statement are fully known at precompile
time.
– Host variables values can be specified at run time (but their data type
must still be precompiled).
• Example:
SELECT lastname, firstname
FROM employee
WHERE empno = :hostvar
– You precompile, bind, and compile statically executed SQL statements
before you run your application.
– Static SQL is best used on databases whose statistics do not change a
Bina Nusantara University
great deal.
8
Static vs. Dynamic SQL
• Dynamic SQL
– Built and executed by an application at run-time.
• Example:
SELECT ?, ? FROM ?
• The names for the columns and tables referenced in a
statement are not known until runtime.
• The access plan is determined at runtime
Bina Nusantara University
9
Static vs. Dynamic SQL
• Performance
– Static SQL will perform better than dynamic SQL since
the access plan in static SQL is performed during
precompile time and not at runtime.
– When working with dynamic SQL, use parameter
markers (?) to reduce the amount of times an access
plan is calculated. (see following example)
Bina Nusantara University
10
Static vs. Dynamic SQL
• Performance (continuation)
– Example:
Case 1:
EXECUTE IMMEDIATELY SELECT name from EMP where dept = 1
EXECUTE IMMEDIATELY SELECT name from EMP where dept = 2
vs.
Case 2:
strcpy(hVStmtDyn, “SELECT name FROM emp WHERE dept = ?");
PREPARE StmtDyn FROM :hVStmtDyn;
EXECUTE StmtDyn USING 1;
EXECUTE StmtDyn USING 2;
In case 1, each statement is treated as different SQL, therefore DB2 will calculate the
access plan for each.
In case 2, there is only one SQL statement:
“SELECT name FROM emp WHERE dept = ?“
Therefore, the access plan will only be calculated once, and cached in the package cache.
Bina Nusantara University
11
Static vs. Dynamic SQL
• Embedded SQL applications support static
& dynamic SQL
– Example of a static SQL in an embedded SQL C program
EXEC SQL SELECT name, dept
INTO :name, :dept
FROM staff WHERE id = 310;
printf( …)
– Example of a dynamic SQL in an embedded SQL C program
strcpy(hostVarStmtDyn,
"UPDATE staff SET salary = salary + 1000 WHERE dept =
?");
EXEC SQL PREPARE StmtDyn FROM :hostVarStmtDyn;
EXEC SQL EXECUTE StmtDyn USING :dept;
Bina Nusantara University
12
CLI / ODBC
• CLI = Call Level Interface
X/Open CLI
ODBC
ISO CLI
International
Standard
DB2 CLI
Bina Nusantara University
13
CLI / ODBC
• DB2 CLI can be used as the ODBC Driver when loaded
by an ODBC Driver Manager
• DB2 CLI conforms to ODBC 3.51
DB2 CLI
ODBC
3.51
Bina Nusantara University
14
CLI / ODBC
Brazil
Indonesia
Application
Application
ODBC Driver Manager
DB2 CLI Driver
Oracle ODBC SQL Server
ODBC Driver
Driver
Oracle
Database
SQL Server
Database
DB2 ODBC
Driver
DB2
Database
DB2 Client
DB2
Database
DB2 Client
Canada
Bina Nusantara University
DB2
Database
15
CLI / ODBC
• To run a CLI/ODBC application all you need is the DB2
CLI driver. This driver is installed from either of these:
– IBM Data Server Client
– IBM Data Server Runtime Client
– IBM Data Server Driver for ODBC and CLI
• To develop a CLI/ODBC application you need the DB2
CLI driver and also the appropriate libraries. These can
be found only on:
– IBM Data Server Client
Bina Nusantara University
16
CLI / ODBC
• CLI/ODBC characteristics:
– The code is easily portable between several
RDBMS vendors
– Unlike embedded SQL, there is no need for a
precompiler or host variables
– It runs dynamic SQL
– It is very popular
Bina Nusantara University
17
CLI/ODBC
CLI/ODBC
Embedded Dynamic
versus
EXEC SQL PREPARE ...
.
SQLPrepare(...);
EXEC SQL EXECUTE ...
...
SQLExecute(...);
...
SQLFetch(...);
SQL statements
EXEC SQL FETCH ...
prepared during
application execution
..
Prepare
Execute
Bina Nusantara University
DB2 or
Other
RDBMS
Prepare
Execute
DB2
18
Java Applications
SQLJ
Application
SQLJ
Run-Time Classes
Remote
Database
Java
Application
Bina Nusantara University
JDBC
DB2 Client
19
JDBC / SQLJ / pureQuery
• JDBC characteristics:
– Like in ODBC, the code is easily portable between several RDBMS
vendors
– Dynamic SQL
– It is very popular
• SQLJ
– Embedded SQL in Java
– Static SQL
– Not that popular
• pureQuery
– Eclipse-based plug-in to manage relational data as objects
– IBM’s paradigm to develop Java database applications
– New since mid-2007, available with IBM Data Studio Developer
Bina Nusantara University
20
JDBC / SQLJ
• Supported drivers are listed in the table below
Driver
Type
Driver Name
Packaged
Supports
Minimum level of
Java required
Type 2
DB2 JDBC Type 2
Linux, UNIX and
(Deprecated)
db2java.zip
JDBC 1.2 and
1.4.2
Type 2
Type 4
IBM Data Server
JDBC and SQLJ
db2jcc.jar
sqlj.zip
JDBC 3.0
1.4.2
db2jcc4.jar
sqlj4.zip
JDBC 4.0 and
6
• Type 2 drivers need to have a DB2 client installed
• Deprecated means it is still supported, but no longer enhanced
Bina Nusantara University
21
OLE DB
• OLE DB is a set of interfaces that provides access to data
stored in diverse sources.
• OLE DB consumers can access a DB2 database with the IBM
OLE DB Provider for DB2:
– Provider name: IBMDADB2
– Supports level 0 of the OLE DB provider specification,
including some additional level 1 interfaces
– Complies with Version 2.7 or later of the Microsoft OLE DB
specification
– An IBM Data Server Client with MDAC must be installed
– If IBMDADB2 is not explicitly specified, Microsoft’s OLE DB
driver (MSDASQL) will be utilized by default
Bina Nusantara University
22
ADO.NET
•
•
•
The .NET Framework provides extensive data access support through ADO.NET.
.NET applications use databases through what's known as a data provider
Three data providers that can work with DB2:
– 1) The ODBC .NET Data provider (not recommended)
– Makes ODBC calls to DB2 data source using DB2 CLI driver.
– It has same keyword support and restrictions as that of DB2 CLI driver
–
Can be used with .NET Framework Version 1.1, 2.0, or 3.0.
– 2) The OLE DB .NET Data provider (not recommended)
– uses IBM DB2 OLE DB Driver (IBMDADB2).
– It has same keyword support and restrictions as that of DB2 OLE DB
driver
– Can be used only with .NET Framework Version 1.1, 2.0, or 3.0.
Bina Nusantara University
23
Download