SlideShare a Scribd company logo
Schedule Today: Feb. 7 (TH) PL/SQL, Embedded SQL, CLI, JDBC. Read Sections 8.1, 8.3-8.5. Feb. 12 (T) Advising Day. No class. Reminder: Midterm is Feb. 14 (TH) Covers material through Feb. 7 (TH) lecture and readings (Chapters 1-3, 5-7, 8.1-8.5). Feb. 19 (T) Object-Relational Systems. Read Sections 4.5, 9.4-9.5. Assignment 5 due. Feb. 21 (TH) Transactions, Authorization. Read Sections 8.6-8.7. Project Part 5 due.
Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent SQL operations. Each embedded SQL statement introduced with  EXEC SQL . Preprocessor converts C + SQL to pure C. SQL statements become procedure calls.
Shared Variables A special place for C declarations of variables that are accessible to both SQL and C. Bracketed by EXEC SQL BEGIN/END DECLARE SECTION; In Oracle Pro/C (not C++) the “brackets” are optional. In C, variables used normally; in SQL, they must be preceded by a colon.
Example Find the price for a given beer at a given bar. Sells( bar ,  beer , price) EXEC SQL BEGIN DECLARE SECTION; char theBar[21], theBeer[21]; float thePrice; EXEC SQL END DECLARE SECTION; . . .   /* assign to theBar and theBeer */ . . . EXEC SQL SELECT price INTO :thePrice FROM Sells WHERE beer = :theBeer AND bar = :theBar; . . .
Cursors Similar to PL/SQL cursors, with some syntactic differences. Example Print Joe’s menu. Sells( bar ,  beer , price) EXEC SQL BEGIN DECLARE SECTION; char theBeer[21]; float thePrice; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = 'Joe''s Bar'; EXEC SQL OPEN CURSOR c; while(1) { EXEC SQL FETCH c INTO :theBeer, :thePrice; if(NOT FOUND) break; /* format and print beer and price */ } EXEC SQL CLOSE CURSOR c;
Oracle Vs. SQL Features SQL expects  FROM  in fetch-statement. SQL defines an array of characters  SQLSTATE  that is set every time the system is called. Errors are signaled there. A failure for a cursor to find any more tuples is signaled there. However, Oracle provides us with a header file  sqlca.h  that declares a  communication area  and defines macros to access it. In particular,  NOT FOUND  is a macro that says “the no-tuple-found signal was set.”
Dynamic SQL Motivation: Embedded SQL is fine for fixed applications, e.g., a program that is used by a sales clerk to book an airline seat. It fails if you try to write a program like  sqlplus , because you have compiled the code for  sqlplus  before you see the SQL statements typed in response to the  SQL>  prompt. Two special statements of embedded SQL: PREPARE  turns a character string into an SQL query. EXECUTE  executes that query.
Example: Sqlplus Sketch EXEC SQL BEGIN DECLARE SECTION; char query[MAX_QUERY_LENGTH]; EXEC SQL END DECLARE SECTION; /* issue SQL> prompt */ /* read user's text into array query */ EXEC SQL PREPARE q FROM :query; EXEC SQL EXECUTE q; /* go back to reissue prompt */ Once prepared, a query can be executed many times. “ Prepare” = optimize the query,  e.g ., find a way to execute it using few disk-page I/O’s. Alternatively,  PREPARE  and  EXECUTE  can be combined into: EXEC SQL EXECUTE IMMEDIATE  :query;
Call-Level Interfaces A more modern approach to the host-language/SQL connection is a  call-level interface , in which the C (or other language) program creates SQL statements as character strings and passes them to functions that are part of a library. Similar to what really happens in embedded SQL implementations. Two major approaches: SQL/CLI (standard of ODBC =  open database connectivity ) and JDBC (Java database connectivity).
CLI In C, library calls let you create a  statement handle  = struct in which you can place an SQL statement. See text.  See also Monjian book for PostgreSQL. Use  SQLPrepare(myHandle,  <statement> ,...)  to make  myHandle  represent the SQL statement in the second argument. Use  SQLExecute(myHandle)  to execute that statement. Example SQLPrepare(handle1,~&quot;SELECT~beer,~price FROM Sells WHERE bar = 'Joe''s Bar'&quot;); SQLExecute(handle1);
Fetching Data To obtain the data returned by an executed query, we: Bind variables to the component numbers of the returned query. SQLBindCol  applies to a handle, column number, and variable, plus other arguments (see text). Fetch, using the handle of the query’s statement. SQLFetch  applies to a handle. Example SQLBindCol(handle1, 1, SQL_CHAR, &theBar,...) SQLBindCol(handle1, 2, SQL_REAL, &thePrice,...) SQLExecute(handle1); ... while(SQLFetch(handle1) != SQL_NO_DATA)  { ... }
JDBC Start with a  Connection  object, obtained from the DBMS (see text). Method  createStatement () returns an object of class  Statement  (if there is no argument) or  PreparedStatement  if there is an SQL statement as argument. Example Statement stat1 = myCon.createStatement(); PreparedStatement stat2 = myCon.createStatement( &quot;SELECT beer, price &quot; + &quot;FROM Sells&quot; + &quot;WHERE bar = 'Joe''s Bar'&quot; ); myCon  is a connection,  stat1  is an “empty” statement object, and  stat2  is a (prepared) statement object that has an SQL statement associated.
Executing Statements JDBC distinguishes queries (statements that return data) from  updates  (statements that only affect the database). Methods  executeQuery () and  executeUpdate () are used to execute these two kinds of SQL statements. They must have an argument if applied to a  Statement , never if applied to a  PreparedStatement . When a query is executed, it returns an object of class  ResultSet . Example stat1.executeUpdate( &quot;INSERT INTO Sells&quot; + &quot;VALUES('Brass Rail', 'Bud', 3.00)&quot; ); ResultSet Menu = stat2.executeQuery();
Getting the Tuples of a  ResultSet Method  Next () applies to a  ResultSet  and moves a  “cursor” to the next tuple in that set. Apply  Next () once to get to the first tuple. Next () returns  FALSE  if there are no more tuples. While a given tuple is the current of the cursor, you can get its  i th component by applying to a  ResultSet  a method of the form  get  X (i),  where  X  is the name for the type of that component. Example while(Menu.Next())  { theBeer = Menu.getString(1); thePrice = Menu.getFloat(2); ... }

More Related Content

PDF
Dynamic websites lec3
ODP
Prabu's sql quries
PPT
PPTX
Oracle: PLSQL Introduction
ODP
My sql Syntax
PPT
1 - Introduction to PL/SQL
PDF
Oracle sql & plsql
DOCX
Integrity and security
Dynamic websites lec3
Prabu's sql quries
Oracle: PLSQL Introduction
My sql Syntax
1 - Introduction to PL/SQL
Oracle sql & plsql
Integrity and security

What's hot (19)

ODP
Open Gurukul Language PL/SQL
PPTX
PL/SQL Fundamentals I
PPTX
4. plsql
PPT
ORACLE PL SQL
PPTX
Database Systems - SQL - DCL Statements (Chapter 3/4)
PPT
Oracle PLSQL Step By Step Guide
PPTX
ORACLE PL SQL FOR BEGINNERS
PPT
SQL- Introduction to PL/SQL
PPT
Sql dml & tcl 2
PPTX
PLSQL Tutorial
PPT
2e data models
PPTX
Packages in PL/SQL
PPTX
Oracle: DDL
PPT
11 Understanding and Influencing the PL/SQL Compilar
PPS
Procedures/functions of rdbms
PPT
02 Writing Executable Statments
DOCX
Forall & bulk binds
PDF
PL/SQL Complete Tutorial. All Topics Covered
Open Gurukul Language PL/SQL
PL/SQL Fundamentals I
4. plsql
ORACLE PL SQL
Database Systems - SQL - DCL Statements (Chapter 3/4)
Oracle PLSQL Step By Step Guide
ORACLE PL SQL FOR BEGINNERS
SQL- Introduction to PL/SQL
Sql dml & tcl 2
PLSQL Tutorial
2e data models
Packages in PL/SQL
Oracle: DDL
11 Understanding and Influencing the PL/SQL Compilar
Procedures/functions of rdbms
02 Writing Executable Statments
Forall & bulk binds
PL/SQL Complete Tutorial. All Topics Covered
Ad

Similar to Slides11 (20)

PPT
JDBC – Java Database Connectivity
PPTX
Cursors, triggers, procedures
PPT
My sql with querys
PDF
Sql tutorial
PDF
Arrays and lists in sql server 2008
PPT
Sqlapi0.1
PPTX
embedded-static-&dynamic
PPT
Chapter09
PPT
PPTX
SQL Server Select Topics
PPT
PLSQL (1).ppt
PDF
[Www.pkbulk.blogspot.com]dbms07
PDF
Access tips access and sql part 2 putting vba and sql together
PPT
L9 l10 server side programming
DOC
Oracle etl openworld
PPT
JDBC for CSQL Database
ODP
Porting Applications From Oracle To PostgreSQL
DOCX
Ass2-Descriptor.docx1 Problem DescriptionThe objective of .docx
PPT
Executing Sql Commands
JDBC – Java Database Connectivity
Cursors, triggers, procedures
My sql with querys
Sql tutorial
Arrays and lists in sql server 2008
Sqlapi0.1
embedded-static-&dynamic
Chapter09
SQL Server Select Topics
PLSQL (1).ppt
[Www.pkbulk.blogspot.com]dbms07
Access tips access and sql part 2 putting vba and sql together
L9 l10 server side programming
Oracle etl openworld
JDBC for CSQL Database
Porting Applications From Oracle To PostgreSQL
Ass2-Descriptor.docx1 Problem DescriptionThe objective of .docx
Executing Sql Commands
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PPT
Teaching material agriculture food technology
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Mushroom cultivation and it's methods.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Machine Learning_overview_presentation.pptx
PDF
August Patch Tuesday
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
MIND Revenue Release Quarter 2 2025 Press Release
SOPHOS-XG Firewall Administrator PPT.pptx
A comparative analysis of optical character recognition models for extracting...
Teaching material agriculture food technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
OMC Textile Division Presentation 2021.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Univ-Connecticut-ChatGPT-Presentaion.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
A comparative study of natural language inference in Swahili using monolingua...
Empathic Computing: Creating Shared Understanding
Group 1 Presentation -Planning and Decision Making .pptx
Mushroom cultivation and it's methods.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Machine Learning_overview_presentation.pptx
August Patch Tuesday

Slides11

  • 1. Schedule Today: Feb. 7 (TH) PL/SQL, Embedded SQL, CLI, JDBC. Read Sections 8.1, 8.3-8.5. Feb. 12 (T) Advising Day. No class. Reminder: Midterm is Feb. 14 (TH) Covers material through Feb. 7 (TH) lecture and readings (Chapters 1-3, 5-7, 8.1-8.5). Feb. 19 (T) Object-Relational Systems. Read Sections 4.5, 9.4-9.5. Assignment 5 due. Feb. 21 (TH) Transactions, Authorization. Read Sections 8.6-8.7. Project Part 5 due.
  • 2. Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent SQL operations. Each embedded SQL statement introduced with EXEC SQL . Preprocessor converts C + SQL to pure C. SQL statements become procedure calls.
  • 3. Shared Variables A special place for C declarations of variables that are accessible to both SQL and C. Bracketed by EXEC SQL BEGIN/END DECLARE SECTION; In Oracle Pro/C (not C++) the “brackets” are optional. In C, variables used normally; in SQL, they must be preceded by a colon.
  • 4. Example Find the price for a given beer at a given bar. Sells( bar , beer , price) EXEC SQL BEGIN DECLARE SECTION; char theBar[21], theBeer[21]; float thePrice; EXEC SQL END DECLARE SECTION; . . . /* assign to theBar and theBeer */ . . . EXEC SQL SELECT price INTO :thePrice FROM Sells WHERE beer = :theBeer AND bar = :theBar; . . .
  • 5. Cursors Similar to PL/SQL cursors, with some syntactic differences. Example Print Joe’s menu. Sells( bar , beer , price) EXEC SQL BEGIN DECLARE SECTION; char theBeer[21]; float thePrice; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = 'Joe''s Bar'; EXEC SQL OPEN CURSOR c; while(1) { EXEC SQL FETCH c INTO :theBeer, :thePrice; if(NOT FOUND) break; /* format and print beer and price */ } EXEC SQL CLOSE CURSOR c;
  • 6. Oracle Vs. SQL Features SQL expects FROM in fetch-statement. SQL defines an array of characters SQLSTATE that is set every time the system is called. Errors are signaled there. A failure for a cursor to find any more tuples is signaled there. However, Oracle provides us with a header file sqlca.h that declares a communication area and defines macros to access it. In particular, NOT FOUND is a macro that says “the no-tuple-found signal was set.”
  • 7. Dynamic SQL Motivation: Embedded SQL is fine for fixed applications, e.g., a program that is used by a sales clerk to book an airline seat. It fails if you try to write a program like sqlplus , because you have compiled the code for sqlplus before you see the SQL statements typed in response to the SQL> prompt. Two special statements of embedded SQL: PREPARE turns a character string into an SQL query. EXECUTE executes that query.
  • 8. Example: Sqlplus Sketch EXEC SQL BEGIN DECLARE SECTION; char query[MAX_QUERY_LENGTH]; EXEC SQL END DECLARE SECTION; /* issue SQL> prompt */ /* read user's text into array query */ EXEC SQL PREPARE q FROM :query; EXEC SQL EXECUTE q; /* go back to reissue prompt */ Once prepared, a query can be executed many times. “ Prepare” = optimize the query, e.g ., find a way to execute it using few disk-page I/O’s. Alternatively, PREPARE and EXECUTE can be combined into: EXEC SQL EXECUTE IMMEDIATE :query;
  • 9. Call-Level Interfaces A more modern approach to the host-language/SQL connection is a call-level interface , in which the C (or other language) program creates SQL statements as character strings and passes them to functions that are part of a library. Similar to what really happens in embedded SQL implementations. Two major approaches: SQL/CLI (standard of ODBC = open database connectivity ) and JDBC (Java database connectivity).
  • 10. CLI In C, library calls let you create a statement handle = struct in which you can place an SQL statement. See text. See also Monjian book for PostgreSQL. Use SQLPrepare(myHandle, <statement> ,...) to make myHandle represent the SQL statement in the second argument. Use SQLExecute(myHandle) to execute that statement. Example SQLPrepare(handle1,~&quot;SELECT~beer,~price FROM Sells WHERE bar = 'Joe''s Bar'&quot;); SQLExecute(handle1);
  • 11. Fetching Data To obtain the data returned by an executed query, we: Bind variables to the component numbers of the returned query. SQLBindCol applies to a handle, column number, and variable, plus other arguments (see text). Fetch, using the handle of the query’s statement. SQLFetch applies to a handle. Example SQLBindCol(handle1, 1, SQL_CHAR, &theBar,...) SQLBindCol(handle1, 2, SQL_REAL, &thePrice,...) SQLExecute(handle1); ... while(SQLFetch(handle1) != SQL_NO_DATA) { ... }
  • 12. JDBC Start with a Connection object, obtained from the DBMS (see text). Method createStatement () returns an object of class Statement (if there is no argument) or PreparedStatement if there is an SQL statement as argument. Example Statement stat1 = myCon.createStatement(); PreparedStatement stat2 = myCon.createStatement( &quot;SELECT beer, price &quot; + &quot;FROM Sells&quot; + &quot;WHERE bar = 'Joe''s Bar'&quot; ); myCon is a connection, stat1 is an “empty” statement object, and stat2 is a (prepared) statement object that has an SQL statement associated.
  • 13. Executing Statements JDBC distinguishes queries (statements that return data) from updates (statements that only affect the database). Methods executeQuery () and executeUpdate () are used to execute these two kinds of SQL statements. They must have an argument if applied to a Statement , never if applied to a PreparedStatement . When a query is executed, it returns an object of class ResultSet . Example stat1.executeUpdate( &quot;INSERT INTO Sells&quot; + &quot;VALUES('Brass Rail', 'Bud', 3.00)&quot; ); ResultSet Menu = stat2.executeQuery();
  • 14. Getting the Tuples of a ResultSet Method Next () applies to a ResultSet and moves a “cursor” to the next tuple in that set. Apply Next () once to get to the first tuple. Next () returns FALSE if there are no more tuples. While a given tuple is the current of the cursor, you can get its i th component by applying to a ResultSet a method of the form get X (i), where X is the name for the type of that component. Example while(Menu.Next()) { theBeer = Menu.getString(1); thePrice = Menu.getFloat(2); ... }