Chapter 8(+4): Application Design and Development APIs Web Departamento de Engenharia Informática Instituto Superior Técnico 1 st Semester 2010/2011 Slides (fortemente) baseados nos slides oficiais do livro c Silberschatz, Korth and Sudarshan.
Outline APIs Web 1 2 3 APIs ODBC JDBC 4 Web The World Wide Web s and the Web
Outline APIs Web 1 2 3 APIs 4 Web
User Interfaces and Tools APIs Web Most database users do not use a query language like Forms Graphical user interfaces Report generators Data analysis tools can embed or execute Many interfaces are Web-based Client-side: Javascript, Applets,... Server-side: Java Server Pages (JSP), Active Server Pages (ASP),...
Application Architectures APIs Web can be built using one of two architectures: Two tier model Application programs running at user site communicate directly with the database Three tier model User programs running at user sites communicate with an application server The application server in turn communicates with the database
Two-Tier Architecture APIs Web
Three-Tier Architecture APIs Web
Two-Tier Web-Based Architecture APIs Web
Two-tier Model APIs Web Example: Java code runs at client site and uses JDBC to communicate with the server Benefits: flexible, need not be restricted to predefined queries Problems: Security: all database operations possible More code shipped to client Not appropriate across organizations, or in large ones like universities
Three-tier Model APIs Web Example: Web client + Java Servlet using JDBC to talk with database server Benefits: Security handled by application at server Simple client Distributed system Problems: Performance issues Network-dependent Only packaged transactions
Outline APIs Web 1 2 3 APIs 4 Web
APIs Web The standard defines embeddings of in a variety of programming languages such as C, Java, and Cobol A language to which queries are embedded is referred to as a host language, and the structures permitted in the host language comprise embedded EXEC statement is used to identify embedded request to the preprocessor EXEC <embedded statement>; Note: this varies by language (for example, the Java embedding uses # {... };)
Example APIs Web Find the names and cities of customers with more than a given amount of dollars in their accounts EXEC BEGIN DECLARE SECTION; double amount; char name[50]; char city[50]; EXEC END DECLARE SECTION;
Example (cont.) APIs Web Find the names and cities of customers with more than a given amount of dollars in their accounts amount = 100; EXEC declare c cursor for select depositor.customer name, customer city from depositor, customer, account where depositor.customer name = customer.customer name and depositoraccount number = account.account number and account.balance > :amount;
Example (cont.) APIs Web Find the names and cities of customers with more than a given amount of dollars in their accounts EXEC open c; do { EXEC fetch c into :name, :city; if (CODE! = 0) break; printf("%s, %s\n", name, city); } while (1); EXEC close c;
Updates through Cursors APIs Web Can update tuples fetched by cursor by declaring that the cursor is for update declare c cursor for select from account where branch name = Perryridge for update To update the tuple at the current location of cursor c update account set balance = balance + 100 where current of c
Dynamic APIs Web Allows programs to construct and submit queries at run time Example: char sqlprog[100] = "update account " "set balance = balance * 1.05 " "where account number =?"; EXEC prepare dynprog from :sqlprog; char account[10] = A-101 ; EXEC execute dynprog using :account; The dynamic program contains a?, which is a place holder for a value that is provided when the program is executed
Outline APIs ODBC JDBC Web 1 2 3 APIs ODBC JDBC 4 Web
APIs APIs ODBC JDBC Web API (application-program interface) for a program to interact with a database server Application makes calls to Connect with the database server Send commands to the database server Fetch tuples of result one-by-one into program variables ODBC (Open Connectivity) works with C, C++, C#,... JDBC (Java Connectivity) works with Java
ODBC APIs ODBC JDBC Web Open DataBase Connectivity (ODBC) standard Standard for application programs to communicate with a database server Application program interface (API) to open a connection with a database, send queries and updates, get back results. such as GUI, spreadsheets, etc. can use ODBC Each database system supporting ODBC provides a driver library that must be linked with the client program. When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action, and fetch results.
Connecting to the APIs ODBC JDBC Web int ODBCexample() { RETCODE error; HENV env; /*environment*/ HDBC conn; /*database connection*/ AllocEnv(&env); AllocConnect(env, conn); Connect(conn, tagus.ist.utl.pt, NTS, user, NTS, userpasswd, NTS); /*program code*/ } Disconnect(conn); FreeConnect(conn); FreeEnv(env);
Executing Statements APIs ODBC JDBC Web char branchname[80]; float balance; int lenout1, lenout2; HSTMT stmt; AllocStmt(conn, &stmt); char sqlquery[] = "select branch name, sum(balance) from account group by branch name"; error = ExecDirect(stmt, sqlquery, NTS); if (error == SUCCESS) { BindCol(stmt, 1, C CHAR, branchname, 80, &lenout1) BindCol(stmt, 2, C FLOAT, &balance, 0, &lenout2); while (Fetch(stmt) >= SUCCESS) { printf ("%s %g", branchname, balance); } } FreeStmt(stmt, DROP);
More ODBC Features APIs ODBC JDBC Web Prepared Statements statements compiled at the database insert into account values(?,?,?) Repeatedly executed with actual values for the placeholders (? ) Metadata features finding all the relations in the database and finding the names and types of columns of a query result or a relation in the database. Control over transactions Can turn off automatic commit on a connection transactions must then be committed or rolled back explicitly
JDBC APIs ODBC JDBC Web JDBC is a Java API for communicating with database systems supporting JDBC supports a variety of features for querying and updating data, and for retrieving query results JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes Model for communicating with the database: Open a connection Create a statement object Execute queries using the statement object to send queries and fetch results Exception mechanism to handle errors
Connecting to the APIs ODBC JDBC Web public static void JDBCexample(String userid,string passwd) { try { Class.forName( com.mysql.jdbc.driver ); Connection conn = DriverManager.getConnection( jdbc:mysql://tagus.ist.utl.pt/database, userid, passwd); Statement stmt = conn.createstatement(); } /*program code*/ stmt.close(); conn.close(); } catch (Exception sqle) {.out.println("exception: } "+ sqle);
Executing Statements APIs ODBC JDBC Web ResultSet rset = stmt.executequery( "select branch name, avg(balance) from account group by branch name"); while (rset.next()) {.out.println(rset.getstring( branch name ) + + rset.getfloat(2)); } try { stmt.executeupdate( "insert into account values( A-9732, Perryridge, 1 } catch (Exception sqle) {.out.println("could not insert tuple. "+ sqle); }
Prepared Statements APIs ODBC JDBC Web PreparedStatement s; s = conn.preparestatement ( "insert into account values(?,?,?)"); s.setstring(1, A-9732 ); s.setstring(2, Perryridge ); s.setfloat(3, 1200); s.executeupdate(); s.close(); Plus all the other features of ODBC...
Outline APIs Web The World Wide Web s and the Web 1 2 3 APIs 4 Web The World Wide Web s and the Web
The World Wide Web APIs Web The World Wide Web s and the Web The Web is a distributed information system based on hypertext Most Web documents are hypertext documents formatted via the HyperText Markup Language (HTML) HTML documents contain text along with font specifications, and other formatting instructions hypertext links to other documents, which can be associated with regions of the text. forms, enabling users to enter data which can then be sent back to the Web server
HTML and HTTP APIs Web The World Wide Web s and the Web HTML provides formatting, hypertext link, and image display features. HTML also provides input features Select from a set of options Pop-up menus, radio buttons, check lists Enter values Text boxes Filled in input sent back to the server, to be acted upon by an executable at the server HyperText Transfer Protocol (HTTP) used for communication with the Web server
Sample HTML Source Code APIs Web The World Wide Web s and the Web <html> <body> <table border cols = 3> <tr><td>a-101</td><td>downtown</td><td>500</td>< <tr><td>a-102</td><td>perryridge</td><td>400</td> <tr><td>a-201</td><td>brighton</td><td>900</td>< </table> <center>the <i>account</i> relation</center> <form action="bankquery" method=get> Select account/loan and enter number<br> <select name="type"> <option value="account" selected>account</option> <option value="loan">loan</option> </select> <input type=text size=5 name="number"> <input type=submit value="submit"> </form> </body> </html>
Sample HTML Display APIs Web The World Wide Web s and the Web
Uniform Resources Locators APIs Web The World Wide Web s and the Web In the Web, functionality of pointers is provided by Uniform Resource Locators (URLs). URL example: http://dev.mysql.com/doc/refman/5.0/en/index.html The first part indicates how the document is to be accessed The second part gives the unique name of a machine on the Internet The rest of the URL identifies the document within the machine The local identification can be: The path name of a file on the machine, or An identifier (path name) of a program, plus arguments to be passed to the program E.g.: http://www.google.pt/search?hl=pt-pt&q=mysql
Web Servers APIs Web The World Wide Web s and the Web A Web server can easily serve as a front end to a variety of information services The document name in a URL may identify an executable program, that, when run, generates a HTML document When a HTTP server receives a request for such a document, it executes the program, and sends back the HTML document that is generated The Web client can pass extra arguments with the name of the document To install a new service on the Web, one simply needs to create and install an executable that provides that service. The Web browser provides a graphical user interface to the information service Common Gateway Interface (CGI): a standard interface between web and application server
HTTP and Sessions APIs Web The World Wide Web s and the Web The HTTP protocol is connectionless That is, once the server replies to a request, the server closes the connection with the client, and forgets all about the request In contrast, Unix logins, and JDBC/ODBC connections stay connected until the client disconnects retaining user authentication and other information Motivation: reduces load on server operating systems have tight limits on number of open connections on a machine Information services need session information E.g. user authentication should be done only once per session Solution: use a cookie
Sessions and Cookies APIs Web The World Wide Web s and the Web A cookie is a small piece of text containing identifying information Sent by server to browser on first interaction Sent by browser to the server that created the cookie on further interactions part of the HTTP protocol Server saves information about cookies it issued, and can use it when serving a request E.g., authentication information, and user preferences Cookies can be stored permanently or for a limited time
Web Interfaces to s APIs Web The World Wide Web s and the Web Web browsers have become the de-facto standard user interface to databases Enable large numbers of users to access databases from anywhere Avoid the need for downloading/installing specialized code, while providing a good graphical user interface Examples: banks, airline and rental car reservations, university course registration and grading, etc. However, static HTML documents are limited: Cannot customize fixed Web documents for individual users; Problematic to update Web documents, especially if multiple Web documents replicate data.
Dynamic Generation of Web Documents APIs Web The World Wide Web s and the Web Solution: generate Web documents dynamically from data stored in a database Can tailor the display based on user information stored in the database Example: tailored ads, tailored weather and local news,... Displayed information is up-to-date Example: stock market information, prices,...
Common Gateway Interface (CGI) APIs Web The World Wide Web s and the Web Standard protocol for interfacing external application software with an information server Certain locations are defined to be served by a CGI program Whenever a request to a matching URL is received, the corresponding program is called However, the overhead of spawning new processes for every request can be overwhelming Solution: use compiled languages use client side scripting integrate language interpreters into the server optimize the server (use caching,...) Note: all this solutions have both advantages and disadvantages
Servlets APIs Web The World Wide Web s and the Web Java Servlet specification defines an API for communication between the Web server and application program E.g. methods to get parameter values and to send HTML text back to client Application program (also called a servlet) is loaded into the Web server Two-tier model Each request spawns a new thread in the Web server thread is closed once the request is serviced Servlet API provides a getsession() method Sets a cookie on first interaction with browser, and uses it to identify session on further interactions Provides methods to store and look-up per-session information E.g. user name, preferences,...
Example Servlet Code APIs Web The World Wide Web s and the Web Public class BankQueryServlet extends HttpServlet { public void doget(httpservletrequest request, HttpServletResponse result) throws ServletException, IOException { String type = request.getparameter( type ); String number = request.getparameter( number ); } } /*...code to find the loan amount/account balance...*/ /*...using JDBC to communicate with the database...*/ /*...we assume the value is stored in the variable balance...*/ result.setcontenttype( text/html ); PrintWriter out = result.getwriter(); out.println( < HEAD >< TITLE > QueryResult < /TITLE >< /H out.println( < BODY > ); out.println( Balanceon + type + number + = +balance); out.println( < /BODY > ); out.close();
Server-Side Scripting APIs Web The World Wide Web s and the Web Server-side scripting simplifies the task of connecting a database to the Web Define a HTML document with embedded executable code/ queries. Input values from HTML forms can be used directly in the embedded code/ queries. When the document is requested, the Web server executes the embedded code/ queries to generate the actual HTML document. Numerous server-side scripting languages JSP, Server-side Javascript, PHP, Jscript,... General purpose scripting languages: Perl, Python,...
Client Side Scripting and Applets APIs Web The World Wide Web s and the Web Browsers can fetch certain scripts (client-side scripts) or programs along with documents, and execute them in safe mode at the client site Javascript Macromedia Flash and Shockwave for animation/games VRML Applets Client-side scripts/programs allow documents to be active E.g., animation by executing programs at the local site E.g. ensure that values entered by users satisfy some correctness checks Permit flexible interaction with the user. Executing programs at the client site speeds up interaction by avoiding many round trips to server
Client Side Scripting and Security APIs Web The World Wide Web s and the Web Security mechanisms needed to ensure that malicious scripts do not cause damage to the client machine Easy for limited capability scripting languages, harder for general purpose programming languages like Java Example: Java s security system ensures that the Java applet code does not make any system calls directly Disallows dangerous actions such as file writes Notifies the user about potentially dangerous actions, and allows the option to abort the program or to continue execution
APIs Web The World Wide Web s and the Web End of Chapter 8(+4)