C20.0046: Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008
Agenda <ul><li>Today: </li></ul><ul><ul><li>midterms, etc </li></ul></ul><ul><ul><li>Proj2, Mysql accounts </li></ul></ul>...
Midterm, etc. <ul><li>Stats: </li></ul><ul><ul><li>Possible: </li></ul></ul><ul><ul><li>Max: </li></ul></ul><ul><ul><li>Me...
proj2 <ul><li>Give relational model (normalized as needed) for your project </li></ul><ul><li>Give interesting queries </l...
NYU Infrastructure <ul><li>Server: sales (sales.stern.nyu.edu) </li></ul><ul><li>To connect, ssh to sales </li></ul><ul><u...
Programming for SQL <ul><li>Today: from procedural languages to SQL </li></ul><ul><ul><li>Pro*C </li></ul></ul><ul><ul><li...
R.A./SQL has limitations <ul><li>Can easily find Alice’s direct subordinates: </li></ul><ul><li>But: find  all  of King’s ...
New topic: SQL Programming <ul><li>Can write SQL queries in a SQL interpreter </li></ul><ul><ul><li>Command prompt </li></...
Using two languages <ul><li>Q: Why not use just one language? </li></ul><ul><li>“ [W]e can quickly dispense with the idea…...
Impedance mismatch problem <ul><li>Big problem, though:  impedance mismatch </li></ul><ul><ul><li>Data structures in our a...
SQL/host interface in  embedded SQL <ul><li>So Q: how to transfer data between? </li></ul><ul><li>A: Shared variables </li...
Programs with Embedded SQL Preprocessor Host language compiler Preprocessor Oracle’s Pro*C Host language compiler gcc Host...
Interface: SQL / Host Language <ul><li>Values get passed through shared variables. </li></ul><ul><ul><li>Colons precede sh...
Embedded SQL example <ul><li>Context: </li></ul><ul><ul><li>Product (pname,  price, quantity, maker) </li></ul></ul><ul><u...
Embedded SQL example: insert void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; /* product-name, c...
Embedded SQL example: look-up int getWindowsPrice() { EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL...
Embedded SQL example: look-up <ul><li>What about search for arbitrary product? </li></ul><ul><li>Q: Will this work? </li><...
Embedded SQL example: look-up int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[...
Cursors (quickly to 32) <ul><li>For product’s price, looked up single (scalar) value </li></ul><ul><li>Q: What if we SELEC...
Cursor loop structure <ul><li>Each time around loop, we </li></ul><ul><ul><li>Do a  FETCH  to obtain next row </li></ul></...
Multiple-row look-up example void productToXML() { EXEC SQL BEGIN DECLARE SECTION; char pn[20], cn[30]; double p; int q; c...
Multiple look-up example printf(&quot;<allProducts>
&quot;); while (1) { EXEC SQL FETCH FROM  crs  INTO  :n, :p, :q,:c; if...
More on Cursors <ul><li>Cursors can traverse both stored tables and queries </li></ul><ul><li>Cursors can modify a relatio...
Cursor on query not table EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = ‘Izzy''s'; EXEC SQL OPEN...
Modifications with cursors <ul><li>As we traverse through result set, can modify  the current row </li></ul><ul><ul><li>Ca...
Modification by cursor example void raisePrices() { EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL E...
Modification by cursor example while (1) { EXEC SQL FETCH FROM  crs  INTO  :p; if ( NO_MORE_TUPLES ) break; if (p < 2000) ...
Limitation of embedded SQL <ul><li>Okay for apps with a fixed set of queries/updates </li></ul><ul><ul><li>Maybe very simp...
Dynamic SQL <ul><li>In  dynamic SQL , query string can be taken as a parameter, passed to DB </li></ul><ul><li>Two steps: ...
Dynamic SQL <ul><li>myquery   =  a SQL variable </li></ul><ul><ul><li>not prefixed by  : </li></ul></ul>void runQuery() { ...
Sketch of sqlplus <ul><li>Something like the  sqlplus  program could be written as a simple dynamic SQL </li></ul><ul><ul>...
Interface: SQL/Host Language <ul><li>Two languages: SQL, host (C/Java/whatever) </li></ul><ul><li>Benefits: </li></ul><ul>...
Programs with Embedded SQL Host language  +  Embedded SQL  Preprocessor Host Language + function calls Host language compi...
Next topic: SQL/CLI (break?) <ul><li>Pro*C converts EXEC SQL code </li></ul><ul><ul><li>--into what? </li></ul></ul><ul><l...
CLI: Java Host language  +  Embedded SQL  Preprocessor Host Language + function calls Host language compiler Executable Pr...
CLI - Overview <ul><li>Similar to what really happens in embedded SQL implementations. </li></ul><ul><li>Major approaches:...
Next topic: JDBC (Java’s CLI) <ul><li>As expected: Java too can talk to SQL </li></ul><ul><li>In some ways: much nicer </l...
JDBC <ul><li>Load JDBC driver for DBMS: </li></ul><ul><li>Obtain a  connection : </li></ul><ul><ul><li>What does this mean...
JDBC <ul><li>Obtain a  statement  object: </li></ul><ul><li>Run a query: </li></ul><ul><li>Or an update: </li></ul>Stateme...
Prepared Statements in JDBC <ul><li>JDBC also supports prepared statements </li></ul><ul><li>Obtain a  PreparedStatement  ...
Obtaining query results <ul><li>“ Cursor” not used, but same idea </li></ul><ul><li>executeQuery() returns a ResultSet: </...
Java & parameter-based SQL <ul><li>Like SQL/CLI in C, Java also supports parameterized queries (why?) </li></ul><ul><li>Pr...
Also: ODBC <ul><li>Used by Microsoft platforms/tools, others </li></ul><ul><li>Access: </li></ul><ul><ul><li>Start | Contr...
Next time <ul><li>DBs on the web: PHP, etc. </li></ul><ul><li>Start working through  PHP/MySQL tutorials on website </li><...
Upcoming SlideShare
Loading in...5
×

PPT

1,299

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,299
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "PPT"

  1. 1. C20.0046: Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008
  2. 2. Agenda <ul><li>Today: </li></ul><ul><ul><li>midterms, etc </li></ul></ul><ul><ul><li>Proj2, Mysql accounts </li></ul></ul><ul><ul><li>SQL programming </li></ul></ul>
  3. 3. Midterm, etc. <ul><li>Stats: </li></ul><ul><ul><li>Possible: </li></ul></ul><ul><ul><li>Max: </li></ul></ul><ul><ul><li>Mean: </li></ul></ul><ul><ul><li>Median: </li></ul></ul><ul><ul><li>Stdev: </li></ul></ul><ul><ul><li>“ Pretty good”: >= </li></ul></ul><ul><li>Go through MT… </li></ul>
  4. 4. proj2 <ul><li>Give relational model (normalized as needed) for your project </li></ul><ul><li>Give interesting queries </li></ul><ul><li>Create tables (indices, etc.) for it in your account </li></ul><ul><li>Should be straightforward </li></ul><ul><ul><li>If you have an account… </li></ul></ul><ul><li>Due: 4/2 </li></ul>
  5. 5. NYU Infrastructure <ul><li>Server: sales (sales.stern.nyu.edu) </li></ul><ul><li>To connect, ssh to sales </li></ul><ul><ul><li>http://www.google.com/search?hl=en&q=ssh+client&btnG=Google+Search </li></ul></ul><ul><ul><li>I use: WinSCP/Putty </li></ul></ul><ul><li>Server: i5.nyu.edu (ssh) </li></ul><ul><ul><li>User/pass: netid / netid (or nyu pass?) </li></ul></ul><ul><li>MySQL user/pass: NetID / NetID </li></ul><ul><li>MySQL shell: mysql </li></ul><ul><ul><li>MySQL host: mysql2 </li></ul></ul><ul><ul><li>See proj3 page for exact command syntax </li></ul></ul><ul><ul><li>sales% mysql -h mysql2 -u my-NetID -p my-UID </li></ul></ul><ul><ul><li>Check that your account works tonight! </li></ul></ul><ul><ul><li>phpMyAdmin: http://i5.nyu.edu/phpMyAdmin/ </li></ul></ul>
  6. 6. Programming for SQL <ul><li>Today: from procedural languages to SQL </li></ul><ul><ul><li>Pro*C </li></ul></ul><ul><ul><li>JDBC </li></ul></ul><ul><li>Next: DB-backed apps on the web </li></ul><ul><ul><li>PHP </li></ul></ul><ul><li>Later: </li></ul><ul><ul><li>PL/SQL, Triggers </li></ul></ul>
  7. 7. R.A./SQL has limitations <ul><li>Can easily find Alice’s direct subordinates: </li></ul><ul><li>But: find all of King’s underlings </li></ul><ul><li>Cannot compute “transitive closure” </li></ul><ul><li>Cannot express in R.A./SQL! </li></ul><ul><li>SQL is not “Turing-Complete” </li></ul>Jones Jones King King NULL Boss Analyst Analyst Manager Manager President Job Ford Scott Blake Jones King Name
  8. 8. New topic: SQL Programming <ul><li>Can write SQL queries in a SQL interpreter </li></ul><ul><ul><li>Command prompt </li></ul></ul><ul><ul><li>SQL*Plus ( sqlplus ) in Oracle </li></ul></ul><ul><ul><li>mysql in MySQL </li></ul></ul><ul><li>Good for experimenting, not for anything non-trivial </li></ul><ul><li>Better: use a standard programming language </li></ul><ul><ul><li>Host language talks to SQL/DB </li></ul></ul>
  9. 9. Using two languages <ul><li>Q: Why not use just one language? </li></ul><ul><li>“ [W]e can quickly dispense with the idea…” (Ullman, p351) </li></ul><ul><li>Q: Why not do everything in SQL? </li></ul><ul><li>A: Not designed as a general-purpose language </li></ul><ul><ul><li>No recursion (no factorial!), not Turing-complete </li></ul></ul><ul><ul><li>No, e.g., Swing library </li></ul></ul><ul><li>Q: Why not do everything in the host lang.? </li></ul><ul><li>A: What Oracle provides is highly non-trivial </li></ul><ul><ul><li>Query interpretation, optimizing </li></ul></ul><ul><ul><li>Queries stay constant across host languages </li></ul></ul><ul><ul><li>Many points of entry </li></ul></ul><ul><li>Germ of OO: modularize </li></ul>
  10. 10. Impedance mismatch problem <ul><li>Big problem, though: impedance mismatch </li></ul><ul><ul><li>Data structures in our app-programming lang. don’t automatically map onto those in SQL </li></ul></ul><ul><ul><ul><li>Different types/representations for data </li></ul></ul></ul><ul><li>In SQL: tables with scalar fields </li></ul><ul><li>In C: scalars, records (containing records…), pointers, arrays </li></ul><ul><li>In Java: scalars, objects, references, arrays </li></ul><ul><li>In Perl: scalars, lists/arrays, hashes/assoc. </li></ul>
  11. 11. SQL/host interface in embedded SQL <ul><li>So Q: how to transfer data between? </li></ul><ul><li>A: Shared variables </li></ul><ul><ul><li>Some vars in the program can be used by SQL </li></ul></ul><ul><ul><li>Prefix var with a : </li></ul></ul><ul><ul><li>After query, look here for received data </li></ul></ul><ul><li>SQL commands embedded in app. code </li></ul><ul><ul><li>Identified by EXEC SQL </li></ul></ul><ul><li>Source code is preprocessed before regular compilation </li></ul><ul><ul><li>Result is (e.g.) a C program with library calls </li></ul></ul>
  12. 12. 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
  13. 13. Interface: SQL / Host Language <ul><li>Values get passed through shared variables. </li></ul><ul><ul><li>Colons precede shared variables in SQL statements </li></ul></ul><ul><ul><li>EXEC SQL demarcates every SQL statement </li></ul></ul><ul><li>The variable SQLSTATE provides error messages and status reports </li></ul><ul><ul><li>“ 00000” ~ success </li></ul></ul><ul><ul><li>“ 02000” ~ tuple not found </li></ul></ul><ul><ul><ul><li>Used in loops </li></ul></ul></ul>EXEC SQL BEGIN DECLARE SECTION; char productName[30]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;
  14. 14. Embedded SQL example <ul><li>Context: </li></ul><ul><ul><li>Product (pname, price, quantity, maker) </li></ul></ul><ul><ul><li>Purchase (buyer, seller, store, pname) </li></ul></ul><ul><ul><li>Company (cname, city) </li></ul></ul><ul><ul><li>Person(name, phone, city) </li></ul></ul><ul><li>Goal 1: Insert a new row in Purchase </li></ul><ul><li>Goal 2: Look up price of product by name </li></ul>
  15. 15. 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); }
  16. 16. Embedded SQL example: look-up int getWindowsPrice() { EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL SELECT price INTO :p FROM Product WHERE Product.name = ‘Windows’; return p; }
  17. 17. Embedded SQL example: look-up <ul><li>What about search for arbitrary product? </li></ul><ul><li>Q: Will this work? </li></ul>int getPrice(char *name) { EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL SELECT price INTO :p FROM Product WHERE Product.name = :name; return p; }
  18. 18. 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 SQL SELECT price INTO :p FROM Product WHERE Product.name = :n; return p; }
  19. 19. Cursors (quickly to 32) <ul><li>For product’s price, looked up single (scalar) value </li></ul><ul><li>Q: What if we SELECT multiple fields? </li></ul><ul><ul><li>E.g., find all info for some product </li></ul></ul><ul><li>A: Just list destination vars separated by commas </li></ul><ul><li>Q: What if find multiple rows ? </li></ul><ul><ul><li>E.g., find all products above a certain price </li></ul></ul><ul><li>Use a cursor to step through the results </li></ul><ul><ul><li>Each result placed in an array </li></ul></ul><ul><li>Using cursors: </li></ul><ul><ul><li>Declare the cursor </li></ul></ul><ul><ul><li>Open the cursor </li></ul></ul><ul><ul><li>Fetch tuples one by one </li></ul></ul><ul><ul><li>Close the cursor </li></ul></ul>
  20. 20. Cursor loop structure <ul><li>Each time around loop, we </li></ul><ul><ul><li>Do a FETCH to obtain next row </li></ul></ul><ul><ul><li>Examine SQLSTATE to check success </li></ul></ul><ul><ul><li>Can say: </li></ul></ul><ul><li>What is NO_MORE_TUPLES ? </li></ul>#define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”)) if(NO_MORE_TUPLES) break;
  21. 21. 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; ...
  22. 22. Multiple look-up example printf(&quot;<allProducts> &quot;); while (1) { EXEC SQL FETCH FROM crs INTO :n, :p, :q,:c; if ( NO_MORE_TUPLES ) break; printf(“<product> ”); printf(“ <name>%s</name> ”, n); printf(“ <price>%d</price> ”, p); printf(“ <quantity>%d</quantity> ”, q); printf(“ <maker>%s</maker> ”, c); printf(“</product> ”); } EXEC SQL CLOSE crs; printf(“</allProducts> ”); }
  23. 23. More on Cursors <ul><li>Cursors can traverse both stored tables and queries </li></ul><ul><li>Cursors can modify a relation as well as read it </li></ul><ul><li>Cursors can be protected against changes to the underlying relations </li></ul><ul><li>Can determine the order in which the cursor will get tuples by the ORDER BY keyword in the SQL query </li></ul><ul><li>The cursor can be a scrolling one: can go forward, backward </li></ul><ul><ul><li>+n, -n, Abs(n), Abs(-n) </li></ul></ul>
  24. 24. 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;
  25. 25. Modifications with cursors <ul><li>As we traverse through result set, can modify the current row </li></ul><ul><ul><li>Can also modify with arb. WHERE clauses </li></ul></ul><ul><li>NB: In regular SQL, usually modify sets of rows ( UPDATE WHERE …) </li></ul><ul><li>With cursors, we update the last row fetched </li></ul><ul><li>Simple example: in RentStab table, we decide we want </li></ul><ul><ul><li>to raise (e.g., by 5%) all our prices </li></ul></ul><ul><ul><li>Unless price > 2000, in which case they’re deleted </li></ul></ul>
  26. 26. 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; ...
  27. 27. 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; }
  28. 28. Limitation of embedded SQL <ul><li>Okay for apps with a fixed set of queries/updates </li></ul><ul><ul><li>Maybe very simple kiosks </li></ul></ul><ul><li>But consider, say, sqlplus or the sqlzoo website </li></ul><ul><ul><li>Processes arbitrary queries from user </li></ul></ul><ul><li>Can we do this with embedded SQL? </li></ul>
  29. 29. Dynamic SQL <ul><li>In dynamic SQL , query string can be taken as a parameter, passed to DB </li></ul><ul><li>Two steps: </li></ul><ul><ul><li>Prepare: compiles/optimizes the string </li></ul></ul><ul><ul><li>Execute: executes the query </li></ul></ul><ul><li>Combine together: EXECUTE IMMEDIATE </li></ul><ul><ul><li>But separate if query is executed many times (why?) </li></ul></ul>
  30. 30. Dynamic SQL <ul><li>myquery = a SQL variable </li></ul><ul><ul><li>not prefixed by : </li></ul></ul>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; }
  31. 31. Sketch of sqlplus <ul><li>Something like the sqlplus program could be written as a simple dynamic SQL </li></ul><ul><ul><li>future hw idea? </li></ul></ul>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 */
  32. 32. Interface: SQL/Host Language <ul><li>Two languages: SQL, host (C/Java/whatever) </li></ul><ul><li>Benefits: </li></ul><ul><ul><li>DB code (SQL is portable) </li></ul></ul><ul><ul><li>SQL, host language focus on own strengths </li></ul></ul><ul><li>SQL code placed in host language code </li></ul><ul><li>SQL and host language have diff. data types </li></ul><ul><ul><li>“ impedance mismatch” </li></ul></ul><ul><ul><li>Data transferred with “shared variables” </li></ul></ul><ul><li>Use cursors to access/modify data </li></ul><ul><li>Error messages placed in SQLSTATE </li></ul>
  33. 33. 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
  34. 34. Next topic: SQL/CLI (break?) <ul><li>Pro*C converts EXEC SQL code </li></ul><ul><ul><li>--into what? </li></ul></ul><ul><li>If we know the API (“Call-Level Interface”), can call library routines by hand </li></ul><ul><li>Is this better or worse? </li></ul><ul><ul><li>Pros & cons </li></ul></ul><ul><li>Won’t cover in depth </li></ul>
  35. 35. 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
  36. 36. CLI - Overview <ul><li>Similar to what really happens in embedded SQL implementations. </li></ul><ul><li>Major approaches: </li></ul><ul><ul><li>SQL/CLI - standard of ODBC </li></ul></ul><ul><ul><li>JDBC - Java database connectivity </li></ul></ul><ul><ul><li>See http://cbbrowne.com/info/middleware.html for many options </li></ul></ul><ul><li>Advantages over embedded SQL: </li></ul><ul><ul><li>Avoid preprocessor-stage, easier to debug </li></ul></ul><ul><ul><li>In th., use same program with several DBMS </li></ul></ul><ul><ul><li>“ write once, run everywhere” </li></ul></ul><ul><li>Disadvantages: </li></ul><ul><ul><li>Must keep up to date with API changes </li></ul></ul><ul><ul><li>DBMS may have conflicting APIs </li></ul></ul>
  37. 37. Next topic: JDBC (Java’s CLI) <ul><li>As expected: Java too can talk to SQL </li></ul><ul><li>In some ways: much nicer </li></ul><ul><li>JDBC is an interface </li></ul><ul><ul><li>Changes very little </li></ul></ul><ul><ul><li>Each vendor writes own plug-in </li></ul></ul><ul><li>Dev. Strategy: write to API, run w/ the DBMS jar </li></ul><ul><ul><li>See http:// servlet.java.sun.com/products/jdbc/drivers for 219 (!) JDBC drivers </li></ul></ul>
  38. 38. JDBC <ul><li>Load JDBC driver for DBMS: </li></ul><ul><li>Obtain a connection : </li></ul><ul><ul><li>What does this mean? </li></ul></ul><ul><ul><li>Find info here: $ORACLE_HOME/network/admin/tnsnames.ora </li></ul></ul>Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;) Connection con = DriverManager.getConnection( &quot;jdbc:oracle:thin:@as19.es.its.nyu.edu:1521:STN2&quot;, username, passwd);
  39. 39. JDBC <ul><li>Obtain a statement object: </li></ul><ul><li>Run a query: </li></ul><ul><li>Or an update: </li></ul>Statement stmt = con.createStatement(); stmt.executeQuery(“SELECT * FROM table”); stmt.executeUpdate(“INSERT INTO mytable ” + “ VALUES(‘abc’, ‘def’)”);
  40. 40. Prepared Statements in JDBC <ul><li>JDBC also supports prepared statements </li></ul><ul><li>Obtain a PreparedStatement object: </li></ul><ul><li>Now execute: </li></ul><ul><ul><li>When better? </li></ul></ul>PreparedStatement ps = con.createStatement( “ SELECT * FROM table”); ps.executeQuery();
  41. 41. Obtaining query results <ul><li>“ Cursor” not used, but same idea </li></ul><ul><li>executeQuery() returns a ResultSet: </li></ul><ul><li>rs.next() advances to new row, returns false if EOF </li></ul><ul><ul><li>getInt(i) returns ith column (if an int!) from current row </li></ul></ul><ul><ul><li>can look up metadata… </li></ul></ul>ResultSet rs = ps.executeQuery(); while (rs.next()) { String val1 = rs.getString(1); int val2 = rs.getInt(2); … }
  42. 42. Java & parameter-based SQL <ul><li>Like SQL/CLI in C, Java also supports parameterized queries (why?) </li></ul><ul><li>Prepare structure of query </li></ul><ul><li>Then can set values </li></ul><ul><li>When better? </li></ul>PreparedStatement ps = conn.prepareStatement( &quot;SELECT * FROM table WHERE f1 = ? and f2 = ?&quot;); ps.setString(1, &quot;abc&quot;); ps.setString(2, &quot;def&quot;); ResultSet rs = ps.executeQuery(); ...
  43. 43. Also: ODBC <ul><li>Used by Microsoft platforms/tools, others </li></ul><ul><li>Access: </li></ul><ul><ul><li>Start | Control Panel | Administrative Tools | Data Sources (ODBC) </li></ul></ul><ul><li>Similar to JDBC </li></ul><ul><li>Won’t cover </li></ul>
  44. 44. Next time <ul><li>DBs on the web: PHP, etc. </li></ul><ul><li>Start working through PHP/MySQL tutorials on website </li></ul><ul><ul><li>Skim relevant parts of ch.7 </li></ul></ul><ul><li>If you (and your group members) can’t log in, let me know ASAP </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×