Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, 8.3-8.5. Feb. 12 (T) Advising.

Slides:



Advertisements
Similar presentations
1 Database-Connection Libraries Call-Level Interface Java Database Connectivity PHP.
Advertisements

Chapter 9 SQL in a Server Environment Call-Level Interface Java Database Connectivity PHP.
UJeffrey D. Ullman uAnfang von: CS145 - Herbst Stanford University uOnline unter: Folien mit weißem Hintergrund wurden.
1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #15 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #16 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Winter 2002Arthur Keller – CS 18012–1 Schedule Today: Feb. 19 (T) u Object-Relational Systems. u Read Sections 4.5, Assignment 5 due. Feb. 21.
Murali Mani SQL with other Programming Languages.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #18 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #19 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Winter 2002Arthur Keller – CS 18010–1 Schedule Today: Feb. 5 (T) u Triggers, PL/SQL. u Read Sections 7.4, 8.2. Assignment 4 due. Feb. 7 (TH) u PL/SQL,
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
Fall 2001Arthur Keller – CS 18014–1 Schedule Nov. 13 (T) Object-Relational, O-R Queries. u Read Sections 4.5, Assignment 6 due. (No office hours.)
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
Dec 11, 2003Murali Mani SQL with other Programming Languages B term 2004: lecture 16.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
CS411 Database Systems Kazuhiro Minami 07: SQL System Aspects.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Announcements Read JDBC Project Step 5, due Monday.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 1 DATADABASE PROGRAMMING 2Chapter 13 from our text.
1 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Chapter 8 Using SQL in an Application. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 CSC 4480 outline Intro (3-9) Static SQL (10-11)
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
SQL Programming SQL in Application Programs
Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
Chapter 7: Constraints and Triggers Foreign Keys Local and Global Constraints Triggers 1.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: CLI & JDBC & Security Ch Ch. 9.6 – Ch 10.1.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL in the real world 1. The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
SQL in the real world 1. The SQL Client-Server Interactions SQL-clientSQL-server Environment SQL-agent Module Connection Session Current catalog and schema.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7.
1 Lecture 11 PL/SQL Slides from
Chap 8. SQL in a Server Environment-PartIII
1st Semester, 2017 Sanghyun Park
SQL Environment.
Introduction to Database Systems, CS420
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Application Development
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Database-Connection Libraries
Embedded SQL Chapter 8.
Presentation transcript:

Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising Day. No class. Reminder: Midterm is Feb. 14 (TH) u Covers material through Feb. 7 (TH) lecture and readings (Chapters 1-3, 5-7, ). Feb. 19 (T) u Object-Relational Systems. u Read Sections 4.5, Assignment 5 due. Feb. 21 (TH) u Transactions, Authorization. u Read Sections Project Part 5 due.

Winter 2002Arthur Keller – CS 18011–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. u SQL statements become procedure calls.

Winter 2002Arthur Keller – CS 18011–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.

Winter 2002Arthur Keller – CS 18011–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;...

Winter 2002Arthur Keller – CS 18011–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;

Winter 2002Arthur Keller – CS 18011–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. u Errors are signaled there. u 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.”

Winter 2002Arthur Keller – CS 18011–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.

Winter 2002Arthur Keller – CS 18011–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. u “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;

Winter 2002Arthur Keller – CS 18011–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).

Winter 2002Arthur Keller – CS 18011–10 CLI In C, library calls let you create a statement handle = struct in which you can place an SQL statement. u See text. See also Monjian book for PostgreSQL. Use SQLPrepare(myHandle,,...) to make myHandle represent the SQL statement in the second argument. Use SQLExecute(myHandle) to execute that statement. Example SQLPrepare(handle1,~"SELECT~beer,~price FROM Sells WHERE bar = 'Joe''s Bar'"); SQLExecute(handle1);

Winter 2002Arthur Keller – CS 18011–11 Fetching Data To obtain the data returned by an executed query, we: 1.Bind variables to the component numbers of the returned query.  SQLBindCol applies to a handle, column number, and variable, plus other arguments (see text). 2.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) {... }

Winter 2002Arthur Keller – CS 18011–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( "SELECT beer, price " + "FROM Sells" + "WHERE bar = 'Joe''s Bar'" ); myCon is a connection, stat1 is an “empty” statement object, and stat2 is a (prepared) statement object that has an SQL statement associated.

Winter 2002Arthur Keller – CS 18011–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. u 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( "INSERT INTO Sells" + "VALUES('Brass Rail', 'Bud', 3.00)" ); ResultSet Menu = stat2.executeQuery();

Winter 2002Arthur Keller – CS 18011–14 Getting the Tuples of a ResultSet Method Next() applies to a ResultSet and moves a “cursor” to the next tuple in that set. u 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 ith 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);... }