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

Slides:



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

1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
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.
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.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
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.
Dec 11, 2003Murali Mani SQL with other Programming Languages B term 2004: lecture 16.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
SQL/PSM. Persistent, Stored Modules ANSI/ISO 1996: SQL/PSM (or PSM or PSM-96) –also part of SQL:2003 Write procedures /functions and store them in the.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
CSCI 6962: Server-side Design and Programming
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
CS411 Database Systems Kazuhiro Minami 07: SQL System Aspects.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
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.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
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)
Database Application Development SQL In Application Code : 1. Embedded SQL 2. Database API’s 3. SQLJ.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Real SQL Programming Embedded SQL Java Database Connectivity 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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
SQL Programming SQL in Application Programs
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
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.
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.
SQL in the real world 1. The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Database Design and Programming Jan Baumbach Adopted from previous slides of Peter Schneider-Kamp.
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.
Web Database Programming Using PHP
CS3220 Web and Internet Programming Database Access with JDBC
Chap 8. SQL in a Server Environment-PartIII
SQL Environment.
Introduction to Database Systems, CS420
JDBC Database Management Database connectivity
CS320 Web and Internet Programming Database Access with JDBC
Web Database Programming Using PHP
Database Design and Programming
SQL/PSM.
CPSC-310 Database Systems
CPSC-310 Database Systems
Web DB Programming: PHP
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
JDBC Example.
Database-Connection Libraries
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Embedded SQL Chapter 8.
Presentation transcript:

1 Database-Connection Libraries Call-Level Interface Java Database Connectivity PHP

2 An Aside: SQL Injection uSQL queries are often constructed by programs. uThese queries may take constants from user input. uCareless code can allow rather unexpected queries to be constructed and executed.

3 Example: SQL Injection uRelation Accounts(name, passwd, acct). uWeb interface: get name and password from user, store in strings n and p, issue query, display account number. SELECT acct FROM Accounts WHERE name = :n AND passwd = :p

4 User (Who Is Not Bill Gates) Types Name: Password: Your account number is gates’ -- who cares? Comment in Oracle

5 The Query Executed SELECT acct FROM Accounts WHERE name = ’gates’ --’ AND passwd = ’who cares?’ All treated as a comment

6 Host/SQL Interfaces Via Libraries uThe third approach to connecting databases to conventional languages is to use library calls. 1.C + CLI 2.Java + JDBC 3.PHP + PEAR/DB

7 Three-Tier Architecture uA common environment for using a database has three tiers of processors: 1.Web servers --- talk to the user. 2.Application servers --- execute the business logic. 3.Database servers --- get what the app servers need from the database.

8 Example: Amazon uDatabase holds the information about products, customers, etc. uBusiness logic includes things like “what do I do after someone clicks ‘checkout’?” wAnswer: Show the “how will you pay for this?” screen.

9 Environments, Connections, Queries uThe database is, in many DB-access languages, an environment. uDatabase servers maintain some number of connections, so app servers can ask queries or perform modifications. uThe app server issues statements : queries and modifications, usually.

10 Diagram to Remember Environment Connection Statement

11 SQL/CLI uInstead of using a preprocessor (as in embedded SQL), we can use a library of functions. wThe library for C is called SQL/CLI = “Call- Level Interface.” wEmbedded SQL’s preprocessor will translate the EXEC SQL … statements into CLI or similar calls, anyway.

12 Data Structures uC connects to the database by structs of the following types: 1.Environments : represent the DBMS installation. 2.Connections : logins to the database. 3.Statements : SQL statements to be passed to a connection. 4.Descriptions : records about tuples from a query, or parameters of a statement.

13 Handles uFunction SQLAllocHandle(T,I,O) is used to create these structs, which are called environment, connection, and statement handles. wT = type, e.g., SQL_HANDLE_STMT. wI = input handle = struct at next higher level (statement < connection < environment). wO = (address of) output handle.

14 Example: SQLAllocHandle SQLAllocHandle(SQL_HANDLE_STMT, myCon, &myStat);  myCon is a previously created connection handle.  myStat is the name of the statement handle that will be created.

15 Preparing and Executing uSQLPrepare(H, S, L) causes the string S, of length L, to be interpreted as a SQL statement and optimized; the executable statement is placed in statement handle H. uSQLExecute(H) causes the SQL statement represented by statement handle H to be executed.

16 Example: Prepare and Execute SQLPrepare(myStat, ”SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’”, SQL_NTS); SQLExecute(myStat); This constant says the second argument is a “null-terminated string”; i.e., figure out the length by counting characters.

17 Direct Execution uIf we shall execute a statement S only once, we can combine PREPARE and EXECUTE with: SQLExecuteDirect(H,S,L); wAs before, H is a statement handle and L is the length of string S.

18 Fetching Tuples uWhen the SQL statement executed is a query, we need to fetch the tuples of the result. wA cursor is implied by the fact we executed a query; the cursor need not be declared. uSQLFetch(H) gets the next tuple from the result of the statement with handle H.

19 Accessing Query Results uWhen we fetch a tuple, we need to put the components somewhere. uEach component is bound to a variable by the function SQLBindCol. wThis function has 6 arguments, of which we shall show only 1, 2, and 4: 1 = handle of the query statement. 2 = column number. 4 = address of the variable.

20 Example: Binding uSuppose we have just done SQLExecute(myStat), where myStat is the handle for query SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’ uBind the result to theBeer and thePrice: SQLBindCol(myStat, 1,, &theBeer,, ); SQLBindCol(myStat, 2,, &thePrice,, );

21 Example: Fetching uNow, we can fetch all the tuples of the answer by: while ( SQLFetch(myStat) != SQL_NO_DATA) { /* do something with theBeer and thePrice */ } CLI macro representing SQLSTATE = = “failed to find a tuple.”

22 JDBC uJava Database Connectivity (JDBC) is a library similar to SQL/CLI, but with Java as the host language. uLike CLI, but with a few differences for us to cover.

23 Making a Connection import java.sql.*; Class.forName(com.mysql.jdbc.Driver); Connection myCon = DriverManager.getConnection(…); The JDBC classes The driver for mySql; others exist URL of the database your name, and password go here. Loaded by forName

24 Statements uJDBC provides two classes: 1.Statement = an object that can accept a string that is a SQL statement and can execute such a string. 2.PreparedStatement = an object that has an associated SQL statement ready to execute.

25 Creating Statements uThe Connection class has methods to create Statements and PreparedStatements. Statement stat1 = myCon.createStatement(); PreparedStatement stat2 = myCon.createStatement( ”SELECT beer, price FROM Sells ” + ”WHERE bar = ’Joe’ ’s Bar’ ” ); createStatement with no argument returns a Statement; with one argument it returns a PreparedStatement.

26 Executing SQL Statements uJDBC distinguishes queries from modifications, which it calls “updates.” uStatement and PreparedStatement each have methods executeQuery and executeUpdate. wFor Statements: one argument: the query or modification to be executed. wFor PreparedStatements: no argument.

27 Example: Update ustat1 is a Statement. uWe can use it to insert a tuple as: stat1.executeUpdate( ”INSERT INTO Sells ” + ”VALUES(’Brass Rail’,’Bud’,3.00)” );

28 Example: Query ustat2 is a PreparedStatement holding the query ”SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’ ”. uexecuteQuery returns an object of class ResultSet – we’ll examine it later. uThe query: ResultSet menu = stat2.executeQuery();

29 Accessing the ResultSet uAn object of type ResultSet is something like a cursor. uMethod next() advances the “cursor” to the next tuple. wThe first time next() is applied, it gets the first tuple. wIf there are no more tuples, next() returns the value false.

30 Accessing Components of Tuples uWhen a ResultSet is referring to a tuple, we can get the components of that tuple by applying certain methods to the ResultSet. uMethod getX (i ), where X is some type, and i is the component number, returns the value of that component. wThe value must have type X.

31 Example: Accessing Components uMenu = ResultSet for query “SELECT beer, price FROM Sells WHERE bar = ’Joe’ ’s Bar’ ”. uAccess beer and price from each tuple by: while ( menu.next() ) { theBeer = Menu.getString(1); thePrice = Menu.getFloat(2); /*something with theBeer and thePrice*/ }

32 PHP uA language to be used for actions within HTML text. uIndicated by. uDB library exists within PEAR (PHP Extension and Application Repository).  Include with include(DB.php).

33 Variables in PHP uMust begin with $. uOK not to declare a type for a variable. uBut you give a variable a value that belongs to a “class,” in which case, methods of that class are available to it.

34 String Values uPHP solves a very important problem for languages that commonly construct strings as values: wHow do I tell whether a substring needs to be interpreted as a variable and replaced by its value? uPHP solution: Double quotes means replace; single quotes means don’t.

35 Example: Replace or Not? $100 = ”one hundred dollars”; $sue = ’You owe me $100.’; $joe = ”You owe me $100.”; uValue of $sue is ’You owe me $100’, while the value of $joe is ’You owe me one hundred dollars’.

36 PHP Arrays uTwo kinds: numeric and associative. uNumeric arrays are ordinary, indexed 0,1,… wExample: $a = array(”Paul”, ”George”, ”John”, ”Ringo”); Then $a[0] is ”Paul”, $a[1] is ”George”, and so on.

37 Associative Arrays uElements of an associative array $a are pairs x => y, where x is a key string and y is any value. uIf x => y is an element of $a, then $a[x] is y.

38 Example: Associative Arrays uAn environment can be expressed as an associative array, e.g.: $myEnv = array( ”phptype” => ”oracle”, ”hostspec” => ” ”database” => ”cs145db”, ”username” => ”ullman”, ”password” => ”notMyPW”);

39 Making a Connection uWith the DB library imported and the array $myEnv available: $myCon = DB::connect($myEnv); Function connect in the DB library Class is Connection because it is returned by DB::connect().

40 Executing SQL Statements uMethod query applies to a Connection object. uIt takes a string argument and returns a result. wCould be an error code or the relation returned by a query.

41 Example: Executing a Query uFind all the bars that sell a beer given by the variable $beer. $beer = ’Bud’; $result = $myCon->query( ”SELECT bar FROM Sells”. ”WHERE beer = $beer ;”); Concatenation in PHP Remember this variable is replaced by its value. Method application

42 Cursors in PHP uThe result of a query is the tuples returned. uMethod fetchRow applies to the result and returns the next tuple, or FALSE if there is none.

43 Example: Cursors while ($bar = $result->fetchRow()) { // do something with $bar }