Week 5: Embedded SQL. Embedded SQL 4. Application Program. Interactive vs. Non-Interactive SQL. Update Statements



Similar documents
Statement Level Interface. Call Level Interface. Static SQL. Status. Connections. Transactions. To connect to an SQL database, use a connect statement

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

The JAVA Way: JDBC and SQLJ

CS346: Database Programming.

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

More SQL: Assertions, Views, and Programming Techniques

12 Embedding SQL in Programming languages

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

12 Embedding SQL in Programming languages

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

SQL is capable in manipulating relational data SQL is not good for many other tasks

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

Real SQL Programming 1

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

SQL and Programming Languages. SQL in Programming Languages. Applications. Approaches

Self-test Database application programming with JDBC

Real SQL Programming. Embedded SQL Call-Level Interface Java Database Connectivity

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

Programming Database lectures for mathema

JDBC (Java / SQL Programming) CS 377: Database Systems

1 SQL Data Types and Schemas

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Database Application Development. Overview. SQL in Application Code. Chapter 6

Firebird. Embedded SQL Guide for RM/Cobol

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

Darshan Institute of Engineering & Technology PL_SQL

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

Embedded SQL programming

Mimer SQL. Programmer s Manual. Version 8.2 Copyright 2000 Mimer Information Technology AB

Database System Concepts

Embedding SQL in High Level Language Programs

1 File Processing Systems

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

A Brief Introduction to MySQL

ODBC Chapter,First Edition

Week 1 Part 1: An Introduction to Database Systems. Databases and DBMSs. Why Use a DBMS? Why Study Databases??

CS/CE 2336 Computer Science II

ODBC Applications: Writing Good Code

Applets, RMI, JDBC Exam Review

Oracle Database: SQL and PL/SQL Fundamentals

N3458: Simple Database Integration in C++11

Java Application Developer Certificate Program Competencies

Using SQL in RPG Programs: An Introduction

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

Chapter 1: Introduction

DEVELOPING MULTITHREADED DATABASE APPLICATION USING JAVA TOOLS AND ORACLE DATABASE MANAGEMENT SYSTEM IN INTRANET ENVIRONMENT

SQL and programming languages

Choosing a Data Model for Your Database

IBM Power Systems Software. The ABCs of Coding High Performance SQL Apps DB2 for IBM i. Presented by Jarek Miszczyk IBM Rochester, ISV Enablement

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle8/ SQLJ Programming

Database Access via Programming Languages

Database programming 20/08/2015. DBprog news. Outline. Motivation for DB programming. Using SQL queries in a program. Using SQL queries in a program

Teradata Database. SQL Reference. Stored Procedures and Embedded SQL

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

ASP.NET Programming with C# and SQL Server

PL/SQL (Cont d) Let s start with the mail_order database, shown here:

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Oracle Database: SQL and PL/SQL Fundamentals

Chapter 2 Database System Concepts and Architecture

HOW-TO. Access Data using BCI. Brian Leach Consulting Limited.

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

InterBase 6. Embedded SQL Guide. Borland/INPRISE. 100 Enterprise Way, Scotts Valley, CA

ODBC Sample Application for Tandem NonStop SQL/MX

5.1 Database Schema Schema Generation in SQL

Java (12 Weeks) Introduction to Java Programming Language

Database System Concepts

Intro to Embedded SQL Programming for ILE RPG Developers

Sources: On the Web: Slides will be available on:

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

14 Triggers / Embedded SQL

Using DOTS as Apache Derby System Test

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

LSINF1124 Projet de programmation

Introduction to PL/SQL Programming

Raima Database Manager 11.0

KB_SQL Programmer s Reference Guide

Database Programming with PL/SQL: Learning Objectives

PostgreSQL Functions By Example

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

DATABASE SYSTEM CONCEPTS AND ARCHITECTURE CHAPTER 2

SQL. Short introduction

IBM soliddb IBM soliddb Universal Cache Version 6.3. Programmer Guide SC

PL/SQL. Database Procedural Programming PL/SQL and Embedded SQL. Procedures and Functions

Oracle SQL, introduced in the previous chapter, is not a language that can be

SQL. Ilchul Yoon Assistant Professor State University of New York, Korea. on tables. describing schema. CSE 532 Theory of Database Systems

Transcription:

Week 5: Embedded SQL Update Statements Embedded SQL Traditional applications often need to embed SQL statements inside the instructions of a procedural programming language (C, COBOL, etc.) Programs with embedded SQL use a pre-compiler to manage SQL statements. Embedded statements are preceded by $ or EXEC SQL or some distinguished token Program variables may be used as parameters in the SQL statements (preceded by : ) select statements producing a single row and update statements can be embedded easily. The SQL environment offers a predefined variable sqlcode which describes the execution status of an SQL statement (=0 if it executed successfully). Embedded SQL 1 Embedded SQL 2 Interactive vs. Non-Interactive SQL Non-interactive SQL: Statements are included in an application program written in a host language such as C, Java, COBOL Interactive SQL: Statements input from terminal; DBMS outputs to screen Interactive SQL is inadequate for most uses: It may be necessary to process the data before output; Amount of data returned not known in advance; SQL has limited expressive power note: not Turing-complete. Embedded SQL 3 Application Program Host language: A conventional programming language (e.g., C, Java) that supplies control structures, computational capabilities, interaction with physical devices. SQL: supplies ability to interact with database. Using the facilities of both: the application program can act as an intermediary between the user at a terminal and the DBMS. Embedded SQL 4 1

Preparation Before any SQL statement is executed, it must be prepared by the DBMS: What indices can be used? In what order should tables be accessed? What constraints should be checked? Decisions are based on schema, table sizes, etc. Result is a query execution plan. Preparation is a complex activity, usually done at run time, justified by the complexity of query processing. Introducing SQL Into the Application SQL statements can be incorporated into an application program in two different ways. Statement Level Interface (SLI): Application program is a mixture of host language statements and SQL statements and directives. Call Level Interface (CLI): Application program is written entirely in host language. SQL statements are values of string variables that are passed as arguments to host language (library) procedures Embedded SQL 5 Embedded SQL 6 Statement Level Interface SQL statements and directives in the application have a special syntax that sets them off from host language constructs e.g., EXEC SQL SQL_statement Pre-compiler scans program and translates SQL statements into calls to host language library procedures that communicate with DBMS. Host language compiler then compiles program. Embedded SQL 7 Statement Level Interface SQL constructs in an application take two forms: Standard SQL statements (static or embedded SQL): Useful when SQL portion of program is known at compile time Directives (dynamic SQL): Useful when SQL portion of program not known at compile time. Application constructs SQL statements at run time as values of host language variables that are manipulated by directives Pre-compiler translates statements and directives into arguments of calls to library procedures. Embedded SQL 8 2

Call Level Interface Application program written entirely in host language (no precompiler) Examples: JDBC, ODBC SQL statements are values of string variables constructed at run time using host language Similar to dynamic SQL Application uses string variables as arguments of library routines that communicate with DBMS e.g. executequery( SQL query statement ) Embedded SQL 9 Static SQL EXEC SQL BEGIN DECLARE S; unsigned long num_enrolled; char crs_code; char SQLSTATE [6]; EXEC SQL END DECLARE SE;. EXEC SQL SELECT C.NumEnrolled INTO :num_enrolled FROM Course C : used to WHERE C.CrsCode = :crs_code; set off host variables Declaration section for host/sql communication. Colon convention for value (WHERE) and result (INTO) parameters. Variables shared by host and SQL Embedded SQL 10 Status EXEC SQL SELECT C.NumEnrolled INTO :num_enrolled FROM Course C WHERE C.CrsCode = :crs_code; if (!strcmp (SQLSTATE, 00000 ) ) { printf ( statement failed ) }; Out parameter In parameter Connections To connect to an SQL database, use a connect statement CONNECT TO database_name AS connection_name USING user_id Embedded SQL 11 Embedded SQL 12 3

Transactions No explicit statement is needed to begin a transaction: A transaction is initiated when the first SQL statement that accesses the database is executed. The mode of transaction execution can be set with SET TRANSACTION READ ONLY ISOLATION LEVEL SERIALIZABLE Transactions are terminated with COMMIT or ROLLBACK statements. Example: Course Deregistration EXEC SQL CONNECT TO :dbserver; if (! strcmp (SQLSTATE, 00000 ) ) exit (1);.. EXEC SQL DELETE FROM Transcript T WHERE T.StudId = :studid AND T.Semester = S2000 AND T.CrsCode = :crscode; if (! strcmp (SQLSTATE, 00000 ) ) EXEC SQL ROLLBACK; else { EXEC SQL UPDATE Course C SET C.Numenrolled = C.Numenrolled 1 WHERE C.CrsCode = :crscode; if (! strcmp (SQLSTATE, 00000 ) ) EXEC SQL ROLLBACK; else EXEC SQL COMMIT; } Embedded SQL 13 Embedded SQL 14 Buffer Mismatch Problem Problem: SQL deals with tables (of arbitrary size); host language program deals with fixed size buffers How is the application to allocate storage for the result of a SELECT statement? Solution: Fetch a single row at a time Space for a single row (number and type of out parameters) can be determined from schema and allocated in application Embedded SQL 15 Cursors Result set set of rows produced by a SELECT statement Cursor pointer to a row in the result set. Cursor operations: Declaration Open execute SELECT to determine result set and initialize pointer Fetch advance pointer and retrieve next row Close deallocate cursor Embedded SQL 16 4

application Cursors (cont d) cursor Result set (or pointers to it) SELECT Base table Example of Cursor Use EXEC SQL DECLARE GetEnroll INSENSITIVE CURSOR FOR SELECT T.StudId, T.Grade cursor is not a schema element FROM Transcript T WHERE T.CrsCode = :crscode AND T.Semester = S2000 ; Reference resolved at compile time, EXEC SQL OPEN GetEnroll; Value substituted at OPEN time if (!strcmp ( SQLSTATE, 00000 )) {... fail exit... };. EXEC SQL FETCH GetEnroll INTO :studid, :grade; while ( SQLSTATE = 00000 ) { process the returned row... EXEC SQL FETCH GetEnroll INTO :studid, :grade; } if (!strcmp ( SQLSTATE, 02000 )) {... fail exit... };. EXEC SQL CLOSE GetEnroll; Embedded SQL 17 Embedded SQL 18 Cursor Types Insensitive cursor: Result set (effectively) computed and stored in a separate table at OPEN time Changes made to base table subsequent to OPEN (by any transaction) do not affect result set Cursor is read-only Cursors that are not insensitive: Specification not part of SQL standard Changes made to base table subsequent to OPEN (by any transaction) can affect result set Cursor is updatable cursor Insensitive Cursor key1 t t t t t t t t key1 t t t t qq t t t t key3 yyyyyyyy key2 xxxxxxxxx key4 zzzzzzzzz Result Set key3 yyyrryyyy key4 zzzzzzzzzz key5 uuuuuuuuu key6 vvvvvvvvv Tuples added after opening the cursor Changes made after opening cursor not seen in the cursor Base Table Embedded SQL 19 Embedded SQL 20 5

Keyset-Driven Cursor Example of a cursor that is not insensitive. Primary key of each row in result set is computed at open time. UPDATE or DELETE of a row in base table by a concurrent transaction between OPEN and FETCH might be seen through cursor. INSERT into base table, however, not seen through cursor. Cursor is updatable. Keyset-Driven Cursor Cursor key1 key1 t t t t t t t t t t key3 key2 xxxxxxxxxxx key4 key3 yyyyyyyyyyy key4 zzzzzzzzzzzz Key set key5 uuuuuuuuuuu key6 vvvvvvvvvvv Base table Tuples added after cursor is open are not seen, but updates to key1, key3, key4 are seen in the cursor. Embedded SQL 21 Embedded SQL 22 Cursors DECLARE cursor-name [INSENSITIVE] [SCROLL] CURSOR FOR table-expr [ ORDER BY column-list ] [ FOR {READ ONLY UPDATE [ OF column-list ] } ] For updatable (not insensitive, not read-only) cursors UPDATE table-name base table SET assignment WHERE CURRENT OF cursor-name DELETE FROM table-name base table WHERE CURRENT OF cursor-name Restriction table-expr must satisfy restrictions of updatable view Scrolling If SCROLL option not specified in cursor declaration, FETCH always moves cursor forward one position If SCROLL option is included in DECLARE CURSOR section, cursor can Get be previous moved tuple in arbitrary ways around result set: FETCH PRIOR FROM GetEnroll INTO :studid, :grade; Also: FIRST, LAST, ABSOLUTE n, RELATIVE n Embedded SQL 23 Embedded SQL 24 6

Stored Procedures Procedure written in a conventional algorithmic language Included as schema element (stored in DBMS) Invoked by the application Advantages: Intermediate data need not be communicated to application (time and cost savings) Procedure s SQL statements prepared in advance Authorization can be done at procedure level Added security since procedure resides in server Applications that call the procedure need not know the details of database schema all database access is encapsulated within the procedure Embedded SQL 25 Dynamic SQL strcpy (tmp, SELECT C.NumEnrolled FROM Course C \ WHERE C.CrsCode =? ) ; EXEC SQL PREPARE st FROM :tmp; EXEC SQL EXECUTE st INTO :num_enrolled USING :crs_code; placeholder st is an SQL variable; names the SQL statement tmp, crscode, num_enrolled are host language variables (note colon notation) crscode is an in parameter; supplies value for placeholder (?) num_enrolled is an out parameter; receives value from C.NumEnrolled Embedded SQL 26 Dynamic SQL PREPARE names SQL statement st and sends it to DBMS for preparation EXECUTE causes the statement named st to be executed Parameters for Static SQL For Static SQL: Names of (host language) parameters are contained in SQL statement and available to pre-compiler. Address and type information in symbol table. Routines for fetching and storing argument values can be generated. Complete statement (with parameter values) sent to DBMS when statement is executed. EXEC SQL SELECT C.NumEnrolled INTO :num_enrolled FROM Course C WHERE C.CrsCode = :crs_code; Embedded SQL 27 Embedded SQL 28 7

Parameters for Dynamic SQL Dynamic SQL: SQL statement constructed at run time when symbol table is no longer present Case 1: Parameters are known at compile time strcpy (tmp, SELECT C.NumEnrolled FROM Course C \ WHERE C.CrsCode =? ) ; EXEC SQL PREPARE st FROM :tmp; Parameters are named in EXECUTE statement: in parameters in USING; out parameters in INTO clauses EXEC SQL EXECUTE st INTO :num_enrolled USING :crs_code; EXECUTE statement is compiled using symbol table fetch() and store() routines generated Parameters for Dynamic SQL (Case 1: parameters known at compile time) Fetch and store routines are executed at client when EXECUTE is executed to communicate argument values with DBMS EXECUTE can be invoked multiple times with different values of in parameters Each invocation uses same query execution plan Values substituted for placeholders by DBMS (in order) at invocation time and statement is executed Embedded SQL 29 Embedded SQL 30 Parameters in Dynamic SQL (parameters supplied at runtime) Case 2: Parameters not known at compile time Example: Statement input from terminal Application cannot parse statement and might not know schema, so it does not have any parameter information EXECUTE statement cannot name parameters in INTO and USING clauses Embedded SQL 31 Parameters in Dynamic SQL (Case 2: parameters supplied at runtime) DBMS determines number and type of parameters after preparing the statement Information stored by DBMS in a descriptor a data structure inside the DBMS, which records the name, type, and value of each parameter Dynamic SQL provides directive GET DESCRIPTOR to get information about parameters (e.g., number, name, type) from DBMS and to fetch value of out parameters Dynamic SQL provides directive SET DESCRIPTOR to supply value to in parameters Embedded SQL 32 8

Descriptors temp = SELECT C.NumEnrolled, C.Name FROM Course C \ WHERE C.CrsCode = CS305 application Descriptor GET DESCRIPTOR 1. Application uses GET DESCRIPTOR to fetch name, type, value 2. Then gets value into appropriate host variable 3. Then processes value 60 NumEnrolled integer Databases Name string DBMS value name type value name type Embedded SQL 33 Dynamic SQL Calls when Descriptors are Used construct SQL statement in temp EXEC SQL PREPARE st FROM :temp; // prepare statement EXEC SQL ALLOCATE DESCRIPTOR desc ; // create descriptor EXEC SQL DESCRIBE OUTPUT st USING SQL DESCRIPTOR desc ; // populate desc with info // about out parameters EXEC SQL EXECUTE st INTO SQL DESCRIPTOR AREA desc ; EXEC SQL GET DESCRIPTOR desc ; // execute statement and // store out values in desc // get out values similar strategy is used for in parameters Embedded SQL 34 Example: Nothing Known at Compile Time sprintf(my_sql_stmt, SELECT * FROM %s WHERE COUNT(*) = 1, table); // table host var; even the table is known only at run time! EXEC SQL PREPARE st FROM :my_sql_stmt; EXEC SQL ALLOCATE DESCRIPTOR st_output ; EXEC SQL DESCRIBE OUTPUT st USING SQL DESCRIPTOR st_output The SQL statement to execute is known only at run time At this point DBMS knows what the exact statement is (including the table name, the number of out parameters, their types) The above statement asks to create descriptors in st_output for all the (now known) out parameters EXEC SQL EXECUTE st INTO SQL DESCRIPTOR st_output ; Example: Getting Meta-Information from a Descriptor // Host var colcount gets the number of out parameters in // the SQL statement described by st_output EXEC SQL GET DESCRIPTOR st_output :colcount = COUNT; // Set host vars coltype, collength, colname with the type, // length, and name of the colnumber s out parameter in // the SQL statement described by st_output EXEC SQL GET DESCRIPTOR st_output VALUE :colnumber; :coltype = TYPE, // predefined integer constants, // such as SQL_CHAR, SQL_FLOAT, :collength = LENGTH, :colname = NAME; Embedded SQL 35 Embedded SQL 36 9

char strdata[1024]; int intdata; switch (coltype) { case SQL_CHAR: Example: Using Meta-Information to Extract Attribute Value EXEC SQL GET DESCRIPTOR st_output VALUE :colnumber strdata=data; break; case SQL_INT: EXEC SQL GET DESCRIPTOR st_output VALUE :colnumber :intdata=data; break; case SQL_FLOAT: } Put the value of attribute colnumber into the variable strdata JDBC Call-level interface (CLI) for executing SQL from a Java program SQL statement is constructed at run time as the value of a Java variable (as in dynamic SQL) JDBC passes SQL statements to the underlying DBMS. Can be interfaced to any DBMS that has a JDBC driver Part of SQL:2003 Embedded SQL 37 Embedded SQL 38 JDBC Run-Time Architecture Executing a Query import java.sql.*; -- import all classes in package java.sql application driver manager Oracle driver SQLServer driver DB/2 driver Oracle database SQLServer database DB/2 database DBMS Class.forName (driver name); // static method of class Class // loads specified driver Connection con = DriverManager.getConnection(Url, Id, Passwd); Static method of class DriverManager; attempts to connect to DBMS If successful, creates a connection object, con, for managing the connection Statement stat = con.createstatement (); Creates a statement object stat Statements have executequery() method Embedded SQL 39 Embedded SQL 40 10

Executing a Query (cont d) String query = SELECT T.StudId FROM Transcript T + WHERE T.CrsCode = cse305 + AND T.Semester = S2000 ; ResultSet res = stat.executequery (query); Creates a result set object, res. Prepares and executes the query. Stores the result set produced by execution in res (analogous to opening a cursor). The query string can be constructed at run time (as above). The input parameters are plugged into the query when the string is formed (as above) Preparing and Executing a Query String query = SELECT T.StudId FROM Transcript T + WHERE T.CrsCode =? AND T.Semester =? ; placeholders PreparedStatement ps = con.preparestatement ( query ); Prepares the statement Creates a prepared statement object, ps, containing the prepared statement Placeholders (?) mark positions of in parameters; special API is provided to plug the actual values in positions indicated by the? s Embedded SQL 41 Embedded SQL 42 Preparing and Executing a Query (cont d) String crs_code, semester; ps.setstring(1, crs_code); ps.setstring(2, semester); // set value of first in parameter // set value of second in parameter ResultSet res = ps.executequery ( ); Creates a result set object, res Executes the query Stores the result set produced by execution in res while ( res.next ( ) ) { // advance the cursor j = res.getint ( StudId ); // fetch output int-value process output value } Embedded SQL 43 Result Sets and Cursors Three types of result sets in JDBC: Forward-only: not scrollable Scroll-insensitive: scrollable; changes made to underlying tables after the creation of the result set are not visible through that result set Scroll-sensitive: scrollable; updates and deletes made to tuples in the underlying tables after the creation of the result set are visible through the set Embedded SQL 44 11

Result Set Statement stat = con.createstatement ( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE ); Any result set type can be declared read-only or updatable CONCUR_UPDATABLE (assuming SQL query satisfies the conditions for updatable views) Updatable: Current row of an updatable result set can be changed or deleted, or a new row can be inserted. Any such change causes changes to the underlying database table res.updatestring ( Name, John ); // change the attribute Name of // current row in the row buffer. res.updaterow ( ); // install changes to the current row buffer // in the underlying database table Handling Exceptions try {...Java/JDBC code... } catch ( SQLException ex ) { exception handling code... } try/catch is the basic structure within which an SQL statement should be embedded If an exception is thrown, an exception object, ex, is created and the catch clause is executed The exception object has methods to print an error message, return SQLSTATE, etc. Embedded SQL 45 Embedded SQL 46 Transactions in JDBC Default for a connection is Transaction boundaries Autocommit mode: each SQL statement is a transaction. To group several statements into a transaction use con.setautocommit (false) Isolation default isolation level of the underlying DBMS To change isolation level use con.settransactionisolationlevel (TRANSACTION_SERIALIZABLE) With autocommit off: transaction is committed using con.commit(). next transaction is automatically initiated (chaining) Transactions on each connection committed separately SQLJ A statement-level interface to Java A dialect of embedded SQL designed specifically for Java Translated by precompiler into Java SQL constructs translated into calls to an SQLJ runtime package, which accesses database through calls to a JDBC driver Part of SQL:2003 Embedded SQL 47 Embedded SQL 48 12

SQLJ SQLJ Example Has some of efficiencies of embedded SQL Compile-time syntax and type checking Use of host language variables More elegant than embedded SQL Has some of the advantages of JDBC Can access multiple DBMSs using drivers SQLJ statements and JDBC calls can be included in the same program #SQL { SELECT C.Enrollment INTO :numenrolled FROM Class C WHERE C.CrsCode = :crscode AND C.Semester = :semester }; Embedded SQL 49 Embedded SQL 50 Example of SQLJ Iterator Similar to JDBC s ResultSet; provides a cursor mechanism #SQL iterator GetEnrolledIter (int studentid, String studgrade); GetEnrolledIter iter1; Method names by which to access the attributes StudentId and Grade #SQL iter1 = { SELECT T.StudentId as studentid, T.Grade as studgrade FROM Transcript T WHERE T.CrsCode = :crscode AND T.Semester = :semester }; Embedded SQL 51 int id; String grade; Iterator Example (cont d) while ( iter1.next( ) ) { id = iter1.studentid(); grade = iter1.studgrade(); process the values in id and grade }; iter1.close(); Embedded SQL 52 13

ODBC Call level interface that is database independent Related to SQL/CLI, part of SQL:1999 Software architecture similar to JDBC with driver manager and drivers Not object oriented Low-level: application must specifically allocate and deallocate storage Sequence of Procedure Calls Needed for ODBC SQLAllocEnv(&henv); // get environment handle SQLAllocConnect(henv, &hdbc); // get connection handle SQLConnect(hdbc, db_name, userid, password); // connect SQLAllocStmt(hdbc, &hstmt); // get statement handle SQLPrepare(hstmt, SQL statement); // prepare SQL statement SQLExecute(hstmt); SQLFreeStmt(hstmt); // free up statement space SQLDisconnect(hdbc); SQLFreeEnv(henv); // free up environment space Embedded SQL 53 Embedded SQL 54 ODBC Features Cursors Statement handle (for example hstmt) is used as name of cursor Status Processing Each ODBC procedure is actually a function that returns status RETCODE retcode1; Retcode1 = SQLConnect ( ) Transactions Can be committed or aborted with SQLTransact (henv, hdbc, SQL_COMMIT) Cursors Fundamental problem with database technology: impedance mismatch traditional programming languages process records one-at-a-time (tupleoriented); SQL processes tuple sets (set-oriented). Cursors solve this problem: A cursor accesses the result of a query in a set-oriented way, returns tuples for the program to process one-by-one. Syntax of cursor definition: declare CursorName [ scroll ] cursor for SelectSQL [ for < read only update [ of Attribute {, Attribute}]>] Embedded SQL 55 Embedded SQL 56 14

Operations on Cursors To execute the query associated with a cursor: open CursorName To extract one tuple from the query result: fetch [ Position from ] CursorName into FetchList To free the cursor, discarding the query result: close CursorName To access the current tuple (when a cursor reads a relation, in order to update it): current of CursorName (in a where clause) Example of Embedded SQL void DisplayDepartmentSalaries(char DeptName[]) { char FirstName[20], Surname[20]; long int Salary; $ declare DeptEmp cursor for select FirstName, Surname, Salary from Employee where Dept = :DeptName; $ open DeptEmp; $ fetch DeptEmp into :FirstName, :Surname, :Salary; printf( Department %s\n,deptname); while (sqlcode == 0) { printf( Name: %s %s,firstname,surname); printf( Salary: %d\n,salary); $ fetch DeptEmp into :FirstName, :Surname, :Salary; } $ close DeptEmp; } Embedded SQL 57 Embedded SQL 58 Dynamic SQL When applications do not know at compile-time the SQL statement to execute, they need dynamic SQL. Major problem: managing the transfer of parameters between the program and the SQL environment. For direct execution: execute immediate SQLStatement For execution preceded by the analysis of the statement: prepare CommandName from SQLStatement followed by: execute CommandName [ into TargetList ] [ using ParameterList ] Procedures SQL-2 allows for the definition of procedures, also known as stored procedures. Stored procedures are part of the schema procedure AssignCity (:Dep char(20),:city char(20)) update Department set City = :City where Name = :Dep SQL-2 does not support the the definition of complex procedures Most systems offer SQL extensions that support complex procedures (e.g., Oracle PL/SQL). Embedded SQL 59 Embedded SQL 60 15

Procedure in Oracle PL/SQL Procedure Debit(ClientAcct char(5),withdr int) is OldAmount integer; NewAmount integer; Threshold integer; begin select Amount,Overdraft into OldAmount, Thresh from BankAcct where AcctNo = ClientAcct for update of Amount; NewAmount := OldAmount - WithDr; if NewAmount > Thresh then update BankAcct set Amount = NewAmount where AcctNo = ClientAcct; else insert into OverDraftExceeded values(clientacct,withdr,sysdate); end if; end Debit; Embedded SQL 61 16