23

Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Embed Size (px)

Citation preview

Page 1: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009
Page 2: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Development OverviewPertemuan 11

Matakuliah : T0413Tahun : 2009

Page 3: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 3

DB2 Application Development OverviewMany 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

installedDatabase applications (in C/C++, .NET, Cobol,

Java, etc)

Data Web Services

Covered in Part 1

of the course

To be covered in Part 2

of the course

Page 4: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 4

DB2 Application Development Overview

Development ToolsIBM Data Studio, RDA, RSA, RAD, Visual

Studio, ZendCore for IBM

Client Server

Operating System

IBM Data Server Client / Driver

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.

Programming Language

DB2 Server

- Triggers

- Stored Procedures

- UDFs

Database

Operating System

Part 2 of the course

Part 1 of the course

Page 5: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 5

Command LineProcessor

Commands

InteractiveSQL

Applications

APIs

EmbeddedSQL

Call LevelInterface

SQL/API

DB2 Engine

AdministrationTools

Control Center

Task Center

Journal

Command Editor

Event Analyzer

Health Center JAVA

Accessing DB2

……

Page 6: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 6

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

Page 7: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 7

Embedded SQL

#include <stdio.h>#include <stdlib.h>..int main(int argc, char** argv){ EXEC SQL BEGIN DECLARE SECTION; char dbname[15] ; EXEC SQL END DECLARE SECTION; .../* connect to a database */ EXEC SQL CONNECT TO :dbname USER :raul USING :psw; if (SQLCODE != 0) { printf ("\n *** Error ***\n");

Only SQL, no C code

Only C code,

no embedded

SQL

Object File Executable File

Database

PackageExecutable SQL with

access pathinformation

hello.sqchello.bnd

hello.c hello.o hello.execompile link

bind hello.bnd

precompile hello.sqc bindfile hello.exe needs theright package to runsuccessfully

Page 8: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 8

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 great deal.

Page 9: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 9

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

Page 10: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 10

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)

Page 11: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 11

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.

Page 12: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 12

Static vs. Dynamic SQL

•Embedded SQL applications support static & dynamic SQL– Example of a static SQL in an embedded SQL C programEXEC SQL SELECT name, dept

INTO :name, :dept

FROM staff WHERE id = 310;

printf( …)

– Example of a dynamic SQL in an embedded SQL C programstrcpy(hostVarStmtDyn,

"UPDATE staff SET salary = salary + 1000 WHERE dept = ?");

EXEC SQL PREPARE StmtDyn FROM :hostVarStmtDyn;

EXEC SQL EXECUTE StmtDyn USING :dept;

Page 13: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 13

CLI / ODBC• CLI = Call Level Interface

X/Open CLI

ODBC

DB2 CLI

ISO CLI International

Standard

Page 14: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 14

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

Page 15: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 15

CLI / ODBC

Application

ODBC Driver Manager

Oracle ODBC Driver

DB2 ODBC Driver

DB2 Client

SQL Server ODBC Driver DB2 Client

Indonesia Brazil

Canada

Application

DB2 CLI Driver

DB2 Database

DB2 Database

DB2 Database

Oracle Database

SQL Server Database

Page 16: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 16

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

Page 17: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 17

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

Page 18: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 18

CLI/ODBCCLI/ODBC versus Embedded Dynamic

SQLPrepare(...);

SQLExecute(...);

SQLFetch(...);

EXEC SQL PREPARE ...

EXEC SQL EXECUTE ...

EXEC SQL FETCH ...

DB2 orOtherRDBMS

DB2

.

...

...

..

SQL statementsprepared during

application execution

Prepare Prepare

Execute Execute

Page 19: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 19

Java Applications

SQLJApplication

SQLJRun-Time Classes

JavaApplication

JDBC DB2 Client

RemoteDatabase

Page 20: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 20

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

Page 21: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 21

JDBC / SQLJ• Supported drivers are listed in the table below

Driver Type

Driver Name Packaged as

Supports Minimum level of SDK for Java required

Type 2 DB2 JDBC Type 2 Driver for Linux, UNIX and Windows (Deprecated)

db2java.zip

JDBC 1.2 and JDBC 2.0

1.4.2

Type 2 and Type 4

IBM Data Server Driver for JDBC and SQLJ

db2jcc.jar and sqlj.zip

JDBC 3.0 compliant

1.4.2

db2jcc4.jar and sqlj4.zip

JDBC 4.0 and earlier

6

• Type 2 drivers need to have a DB2 client installed• Deprecated means it is still supported, but no longer enhanced

Page 22: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 22

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

Page 23: Development Overview Pertemuan 11 Matakuliah: T0413 Tahun: 2009

Bina Nusantara University 23

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.