Docsity

Database Programming - Database Management Systems - Slides | CSCI 4560, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download Database Programming - Database Management Systems - Slides | CSCI 4560 and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

CSCI 4560

Embedded SQL

OCCI

JDBC

Database Management Systems

Topic: Database Programming

Zhijiang Dong

Dept. of Computer Science Middle Tennessee State University

Based on the slides coming with the textbook

CSCI 4560

Embedded SQL

OCCI

JDBC

Topic: Database Programming

1 Embedded SQL

2 OCCI

3 JDBC

CSCI 4560

Embedded SQL

OCCI

JDBC

Database Programming

Objective

To access a database from an application program (as opposed to interactive interfaces)

An interactive interface is convenient but not sufficient

A majority of database operations are made thru application programs (increasingly thru web applications)

CSCI 4560

Embedded SQL

OCCI

JDBC

Database Programming Approaches

Embedded commands

Database commands are embedded in a general-purpose programming language

Library of database functions

Available to the host language for database calls; known as an API (Application Program Interface)

A brand new, full-fledged language

Minimizes impedance mismatch

CSCI 4560

Embedded SQL

OCCI

JDBC

Steps in Database Programming

1 Client program opens a connection to the database server

2 Client program submits queries to and/or updates the

database

3 Client program retrieves query result or feedback from the

database

4 When database access is no longer needed, client program

closes (terminates) the connection

CSCI 4560

Embedded SQL

OCCI

JDBC

Topic: Database Programming

1 Embedded SQL

2 OCCI

3 JDBC

CSCI 4560

Embedded SQL

OCCI

JDBC

Embedded SQL

An embedded SQL statement is distinguished from the host

language statements by enclosing it between EXEC SQL or

EXEC SQL BEGIN and a matching END-EXEC or EXEC

SQL END

Syntax may vary with language

Shared variables (used in both languages) usually prefixed with a colon (:) in SQL

CSCI 4560

Embedded SQL

OCCI

JDBC

Shared Variable Declaration

Variables inside DECLARE are shared and can appear

(while prefixed by a colon) in SQL statements

SQLCODE is used to communicate errors/exceptions

between the database and the program

Example:

int loop; EXEC SQL BEGIN DECLARE SECTION; varchar dname[16], fname[16], ...; char ssn[10], bdate[11], ...; int dno, dnumber, SQLCODE, ...; EXEC SQL END DECLARE SECTION;

CSCI 4560

Embedded SQL

OCCI

JDBC

Establishing Connection

Connection:

CONNECT TO server-name AS connection-name AUTHORIZATION user-account-info;

Disconnection: DISCONNECT connection-name;

CSCI 4560

Embedded SQL

OCCI

JDBC

Execute a SQL Statement

EXEC SQL

select FNAME, LNAME, ADDRESS, SALARY into :fname, :lname, :address, :salary from EMPLOYEE where SSN == :ssn; if (SQLCODE == 0) printf(fname, ...); else printf(“SSN does not exist: ”, ssn); END-EXEC

SQLCODE holds the value returned after the most recently attempted SQL operation

0 after a successful operation,

-1 after an error, or

100 after all requested rows have been fetched.

INTO clause: specifies the program variables into which attribute values from the database are retrieved.

CSCI 4560

Embedded SQL

OCCI

JDBC

Retrieve Multiple Tuples: Example

EXEC SQL

select Dnumber into :dnumber from DEPARTMENT where Dname = :dname; EXEC SQL DECLARE EMP CURSOR FOR select Ssn, Fname, Minit, Lname, Salary from EMPLOYEE where Dno = :dnumber FOR UPDATE OF Salary; EXEC SQL OPEN EMP; EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary; while ( SQLCODE == 0 ) { printf("Employee name is: ", Fname, Minit, Lname); prompt("Enter the raise amount: ", raise); EXEC SQL UPDATE EMPLOYEE set Salary = Salary + :raise where CURRENT OF EMP; EXEC SQL FETCH from EMP into :ssn, :fname, :minit, :lname, :salary; }

EXEC SQL CLOSE EMP;

CSCI 4560

Embedded SQL

OCCI

JDBC

Dynamic SQL

Objective:

Composing and executing new (not previously compiled) SQL statements at run-time a program accepts SQL statements from the keyboard at run-time a point-and-click operation translates to certain SQL query

Dynamic update is relatively simple; dynamic query can be

complex

Composing and executing new (not previously compiled) SQL statements at run-time because the type and number of retrieved attributes are unknown at compile time

CSCI 4560

Embedded SQL

OCCI

JDBC

Topic: Database Programming

1 Embedded SQL

2 OCCI

3 JDBC

CSCI 4560

Embedded SQL

OCCI

JDBC

Introduction

C++ API to access Oracle database

Designed as small set of well encapsulated classes and

interfaces for ease of use

Extensive features for relational access, object-relational

access and scalability

Introduced in 9i, growing customer base