M.P. Johnson, DBMS, Stern/NYU, Spring 20081 C20.0046: Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008.

Slides:



Advertisements
Similar presentations
Chapter 9 SQL in a Server Environment Call-Level Interface Java Database Connectivity PHP.
Advertisements

CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
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.
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, Sp20041 C : Database Management Systems Lecture #17 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.
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.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #19 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY Fall, 2004.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
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.
Embedded SQL Direct SQL is rarely used: usually, SQL is embedded in some application code. We need some method to reference SQL statements. But: there.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY Fall, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
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
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.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Announcements Read JDBC Project Step 5, due Monday.
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)
CS 405G: Introduction to Database Systems Database programming.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Advanced SQL: Cursors & Stored Procedures
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections –
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.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
1 Database Application Development Chapter 6 Sections –
Database Access Using JDBC BCIS 3680 Enterprise Programming.
SQL Programming SQL in Application Programs
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
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.
1 Lecture 8: SQL Programming and Transactions Friday, January 24, 2003.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
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.
Introduction to SQL Programming Techniques
SQL Environment.
Introduction to Database Systems, CS420
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
CPSC-310 Database Systems
Database Application Development
Lecture 06: SQL Systems Aspects
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Lecture 17: Systems Aspects of SQL
Database-Connection Libraries
Lecture 05: SQL Systems Aspects
Embedded SQL Chapter 8.
Presentation transcript:

M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008

M.P. Johnson, DBMS, Stern/NYU, Spring Agenda Today:  midterms, etc  Proj2, Mysql accounts  SQL programming

M.P. Johnson, DBMS, Stern/NYU, Spring Midterm, etc. Stats:  Possible:  Max:  Mean:  Median:  Stdev:  “Pretty good”: >= Go through MT…

M.P. Johnson, DBMS, Stern/NYU, Spring proj2 Give relational model (normalized as needed) for your project Give interesting queries Create tables (indices, etc.) for it in your account Should be straightforward  If you have an account… Due: 4/2

M.P. Johnson, DBMS, Stern/NYU, Spring NYU Infrastructure Server: sales (sales.stern.nyu.edu) To connect, ssh to sales  e+Search e+Search  I use: WinSCP/Putty Server: i5.nyu.edu (ssh)  User/pass: netid / netid (or nyu pass?) MySQL user/pass: NetID / NetID MySQL shell: mysql  MySQL host: mysql2  See proj3 page for exact command syntax sales% mysql -h mysql2 -u my-NetID -p my-UID  Check that your account works tonight!  phpMyAdmin:

M.P. Johnson, DBMS, Stern/NYU, Spring Programming for SQL Today: from procedural languages to SQL  Pro*C  JDBC Next: DB-backed apps on the web  PHP Later:  PL/SQL, Triggers

M.P. Johnson, DBMS, Stern/NYU, Spring R.A./SQL has limitations Can easily find Alice’s direct subordinates: But: find all of King’s underlings Cannot compute “transitive closure” Cannot express in R.A./SQL! SQL is not “Turing-Complete” NameJobBoss KingPresidentNULL JonesManagerKing BlakeManagerKing FordAnalystJones ScottAnalystJones

M.P. Johnson, DBMS, Stern/NYU, Spring New topic: SQL Programming Can write SQL queries in a SQL interpreter  Command prompt  SQL*Plus (sqlplus) in Oracle  mysql in MySQL Good for experimenting, not for anything non-trivial Better: use a standard programming language  Host language talks to SQL/DB

M.P. Johnson, DBMS, Stern/NYU, Spring Using two languages Q: Why not use just one language? “[W]e can quickly dispense with the idea…” (Ullman, p351) Q: Why not do everything in SQL? A: Not designed as a general-purpose language  No recursion (no factorial!), not Turing-complete  No, e.g., Swing library Q: Why not do everything in the host lang.? A: What Oracle provides is highly non-trivial  Query interpretation, optimizing  Queries stay constant across host languages  Many points of entry Germ of OO: modularize

M.P. Johnson, DBMS, Stern/NYU, Spring Impedance mismatch problem Big problem, though: impedance mismatch  Data structures in our app-programming lang. don’t automatically map onto those in SQL Different types/representations for data In SQL: tables with scalar fields In C: scalars, records (containing records…), pointers, arrays In Java: scalars, objects, references, arrays In Perl: scalars, lists/arrays, hashes/assoc.

M.P. Johnson, DBMS, Stern/NYU, Spring SQL/host interface in embedded SQL So Q: how to transfer data between? A: Shared variables  Some vars in the program can be used by SQL  Prefix var with a :  After query, look here for received data SQL commands embedded in app. code  Identified by EXEC SQL Source code is preprocessed before regular compilation  Result is (e.g.) a C program with library calls

M.P. Johnson, DBMS, Stern/NYU, Spring Programs with Embedded SQL Preprocessor Host language compiler Preprocessor Oracle’s Pro*C Host language compiler gcc Host language + Embedded SQL prog.pc Host Language + function calls prog.c Executable a.out

M.P. Johnson, DBMS, Stern/NYU, Spring Interface: SQL / Host Language Values get passed through shared variables.  Colons precede shared variables in SQL statements  EXEC SQL demarcates every SQL statement The variable SQLSTATE provides error messages and status reports  “00000” ~ success  “02000” ~ tuple not found Used in loops EXEC SQL BEGIN DECLARE SECTION; char productName[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL BEGIN DECLARE SECTION; char productName[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

M.P. Johnson, DBMS, Stern/NYU, Spring Embedded SQL example Context:  Product (pname, price, quantity, maker)  Purchase (buyer, seller, store, pname)  Company (cname, city)  Person(name, phone, city) Goal 1: Insert a new row in Purchase Goal 2: Look up price of product by name

M.P. Johnson, DBMS, Stern/NYU, Spring Embedded SQL example: insert void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; /* product-name, company-name */ double p, int q; /* price, quantity */ char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for name, price and company somehow */ EXEC SQL INSERT INTO Product(pname, price, quantity, maker) VALUES (:pn, :p, :q, :cn); } void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; /* product-name, company-name */ double p, int q; /* price, quantity */ char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* get values for name, price and company somehow */ EXEC SQL INSERT INTO Product(pname, price, quantity, maker) VALUES (:pn, :p, :q, :cn); }

M.P. Johnson, DBMS, Stern/NYU, Spring Embedded SQL example: look-up int getWindowsPrice() { EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = ‘Windows’; return p; } int getWindowsPrice() { EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = ‘Windows’; return p; }

M.P. Johnson, DBMS, Stern/NYU, Spring Embedded SQL example: look-up What about search for arbitrary product? Q: Will this work? int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = :name; return p; } int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = :name; return p; }

M.P. Johnson, DBMS, Stern/NYU, Spring Embedded SQL example: look-up int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; strcpy(n, name); /* copy name to local var */ EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = :n; return p; } int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; strcpy(n, name); /* copy name to local var */ EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = :n; return p; }

M.P. Johnson, DBMS, Stern/NYU, Spring Cursors (quickly to 32) For product’s price, looked up single (scalar) value Q: What if we SELECT multiple fields?  E.g., find all info for some product A: Just list destination vars separated by commas Q: What if find multiple rows?  E.g., find all products above a certain price Use a cursor to step through the results  Each result placed in an array Using cursors: 1. Declare the cursor 2. Open the cursor 3. Fetch tuples one by one 4. Close the cursor

M.P. Johnson, DBMS, Stern/NYU, Spring Cursor loop structure Each time around loop, we  Do a FETCH to obtain next row  Examine SQLSTATE to check success  Can say: What is NO_MORE_TUPLES? #define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”)) if(NO_MORE_TUPLES) break;

M.P. Johnson, DBMS, Stern/NYU, Spring Multiple-row look-up example void productToXML() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; double p; int q; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT pname, price, quantity, maker FROM Product; EXEC SQL OPEN crs;... void productToXML() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; double p; int q; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT pname, price, quantity, maker FROM Product; EXEC SQL OPEN crs;...

M.P. Johnson, DBMS, Stern/NYU, Spring Multiple look-up example printf(" \n"); while (1) { EXEC SQL FETCH FROM crs INTO :n, :p, :q,:c; if (NO_MORE_TUPLES) break; printf(“ \n”); printf(“ %s \n”, n); printf(“ %d \n”, p); printf(“ %d \n”, q); printf(“ %s \n”, c); printf(“ \n”); } EXEC SQL CLOSE crs; printf(“ \n”); } printf(" \n"); while (1) { EXEC SQL FETCH FROM crs INTO :n, :p, :q,:c; if (NO_MORE_TUPLES) break; printf(“ \n”); printf(“ %s \n”, n); printf(“ %d \n”, p); printf(“ %d \n”, q); printf(“ %s \n”, c); printf(“ \n”); } EXEC SQL CLOSE crs; printf(“ \n”); }

M.P. Johnson, DBMS, Stern/NYU, Spring More on Cursors Cursors can traverse both stored tables and queries Cursors can modify a relation as well as read it Cursors can be protected against changes to the underlying relations Can determine the order in which the cursor will get tuples by the ORDER BY keyword in the SQL query The cursor can be a scrolling one: can go forward, backward  +n, -n, Abs(n), Abs(-n)

M.P. Johnson, DBMS, Stern/NYU, Spring Cursor on query not table EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = ‘Izzy''s'; 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; EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = ‘Izzy''s'; 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;

M.P. Johnson, DBMS, Stern/NYU, Spring Modifications with cursors As we traverse through result set, can modify the current row  Can also modify with arb. WHERE clauses NB: In regular SQL, usually modify sets of rows (UPDATE WHERE …) With cursors, we update the last row fetched Simple example: in RentStab table, we decide we want  to raise (e.g., by 5%) all our prices  Unless price > 2000, in which case they’re deleted

M.P. Johnson, DBMS, Stern/NYU, Spring Modification by cursor example void raisePrices() { EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT price FROM RentStab; EXEC SQL OPEN crs;... void raisePrices() { EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT price FROM RentStab; EXEC SQL OPEN crs;...

M.P. Johnson, DBMS, Stern/NYU, Spring Modification by cursor example while (1) { EXEC SQL FETCH FROM crs INTO :p; if (NO_MORE_TUPLES) break; if (p < 2000) EXEC SQL UPDATE RentStab SET price = 1.05*price; WHERE CURRENT OF RentStab; else EXEC SQL DELETE FROM RentStab WHERE CURRENT OF RentStab; } EXEC SQL CLOSE crs; } while (1) { EXEC SQL FETCH FROM crs INTO :p; if (NO_MORE_TUPLES) break; if (p < 2000) EXEC SQL UPDATE RentStab SET price = 1.05*price; WHERE CURRENT OF RentStab; else EXEC SQL DELETE FROM RentStab WHERE CURRENT OF RentStab; } EXEC SQL CLOSE crs; }

M.P. Johnson, DBMS, Stern/NYU, Spring Limitation of embedded SQL Okay for apps with a fixed set of queries/updates  Maybe very simple kiosks But consider, say, sqlplus or the sqlzoo website  Processes arbitrary queries from user Can we do this with embedded SQL?

M.P. Johnson, DBMS, Stern/NYU, Spring Dynamic SQL In dynamic SQL, query string can be taken as a parameter, passed to DB Two steps:  Prepare: compiles/optimizes the string  Execute: executes the query Combine together: EXECUTE IMMEDIATE  But separate if query is executed many times (why?)

M.P. Johnson, DBMS, Stern/NYU, Spring Dynamic SQL myquery = a SQL variable  not prefixed by : void runQuery() { EXEC SQL BEGIN DECLARE SECTION; char *command; EXEC SQL END DECLARE SECTION; /* command set to some query string */ EXEC SQL PREPARE myquery FROM :command; EXEC SQL EXECUTE myquery; /* or just: */ EXEC SQL EXECUTE IMMEDIATE :command; } void runQuery() { EXEC SQL BEGIN DECLARE SECTION; char *command; EXEC SQL END DECLARE SECTION; /* command set to some query string */ EXEC SQL PREPARE myquery FROM :command; EXEC SQL EXECUTE myquery; /* or just: */ EXEC SQL EXECUTE IMMEDIATE :command; }

M.P. Johnson, DBMS, Stern/NYU, Spring Sketch of sqlplus Something like the sqlplus program could be written as a simple dynamic SQL  future hw idea? 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 EXECUTE IMMEDIATE :query; /* go back to reissue prompt */ 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 EXECUTE IMMEDIATE :query; /* go back to reissue prompt */

M.P. Johnson, DBMS, Stern/NYU, Spring Interface: SQL/Host Language Two languages: SQL, host (C/Java/whatever) Benefits:  DB code (SQL is portable)  SQL, host language focus on own strengths SQL code placed in host language code SQL and host language have diff. data types  “impedance mismatch”  Data transferred with “shared variables” Use cursors to access/modify data Error messages placed in SQLSTATE

M.P. Johnson, DBMS, Stern/NYU, Spring Programs with Embedded SQL Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Executable Preprocessor Host language compiler Oracle’s Pro*C gcc prog.pc prog.c a.out

M.P. Johnson, DBMS, Stern/NYU, Spring Next topic: SQL/CLI (break?) Pro*C converts EXEC SQL code  --into what? If we know the API (“Call-Level Interface”), can call library routines by hand Is this better or worse?  Pros & cons Won’t cover in depth

M.P. Johnson, DBMS, Stern/NYU, Spring CLI: Java Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Executable Preprocessor Host language compiler Oracle’s Pro*C javac + jar prog.pc Prog.java Proj.class

M.P. Johnson, DBMS, Stern/NYU, Spring CLI - Overview Similar to what really happens in embedded SQL implementations. Major approaches:  SQL/CLI - standard of ODBC  JDBC - Java database connectivity  See for many optionshttp://cbbrowne.com/info/middleware.html Advantages over embedded SQL:  Avoid preprocessor-stage, easier to debug  In th., use same program with several DBMS  “write once, run everywhere” Disadvantages:  Must keep up to date with API changes  DBMS may have conflicting APIs

M.P. Johnson, DBMS, Stern/NYU, Spring Next topic: JDBC (Java’s CLI) As expected: Java too can talk to SQL In some ways: much nicer JDBC is an interface  Changes very little  Each vendor writes own plug-in Dev. Strategy: write to API, run w/ the DBMS jar  See for 219 (!) JDBC drivershttp://servlet.java.sun.com/products/jdbc/drivers

M.P. Johnson, DBMS, Stern/NYU, Spring JDBC 1. Load JDBC driver for DBMS: 2. Obtain a connection: What does this mean? Find info here: $ORACLE_HOME/network/admin/tnsnames.ora Class.forName("oracle.jdbc.driver.OracleDriver") Connection con = DriverManager.getConnection( username, passwd); Connection con = DriverManager.getConnection( username, passwd);

M.P. Johnson, DBMS, Stern/NYU, Spring JDBC 3. Obtain a statement object: 4. Run a query: Or an update: Statement stmt = con.createStatement(); stmt.executeQuery(“SELECT * FROM table”); stmt.executeUpdate(“INSERT INTO mytable ” + “VALUES(‘abc’, ‘def’)”); stmt.executeUpdate(“INSERT INTO mytable ” + “VALUES(‘abc’, ‘def’)”);

M.P. Johnson, DBMS, Stern/NYU, Spring Prepared Statements in JDBC JDBC also supports prepared statements 3. Obtain a PreparedStatement object: 4. Now execute:  When better? PreparedStatement ps = con.createStatement( “SELECT * FROM table”); PreparedStatement ps = con.createStatement( “SELECT * FROM table”); ps.executeQuery();

M.P. Johnson, DBMS, Stern/NYU, Spring Obtaining query results “Cursor” not used, but same idea executeQuery() returns a ResultSet: rs.next() advances to new row, returns false if EOF  getInt(i) returns ith column (if an int!) from current row  can look up metadata… ResultSet rs = ps.executeQuery(); while (rs.next()) { String val1 = rs.getString(1); int val2 = rs.getInt(2); … } while (rs.next()) { String val1 = rs.getString(1); int val2 = rs.getInt(2); … }

M.P. Johnson, DBMS, Stern/NYU, Spring Java & parameter-based SQL Like SQL/CLI in C, Java also supports parameterized queries (why?) 1. Prepare structure of query 2. Then can set values When better? PreparedStatement ps = conn.prepareStatement( "SELECT * FROM table WHERE f1 = ? and f2 = ?"); ps.setString(1, "abc"); ps.setString(2, "def"); ResultSet rs = ps.executeQuery();... PreparedStatement ps = conn.prepareStatement( "SELECT * FROM table WHERE f1 = ? and f2 = ?"); ps.setString(1, "abc"); ps.setString(2, "def"); ResultSet rs = ps.executeQuery();...

M.P. Johnson, DBMS, Stern/NYU, Spring Also: ODBC Used by Microsoft platforms/tools, others Access:  Start | Control Panel | Administrative Tools | Data Sources (ODBC) Similar to JDBC Won’t cover

M.P. Johnson, DBMS, Stern/NYU, Spring Next time DBs on the web: PHP, etc. Start working through PHP/MySQL tutorials on website  Skim relevant parts of ch.7 If you (and your group members) can’t log in, let me know ASAP