Download presentation
Presentation is loading. Please wait.
Published byAron Parks Modified over 9 years ago
1
1 Section 6 Embedded SQL
2
6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases
3
6-3 CA306 Embedded SQL 6.1Embedded SQL Embedding SQL from any real database system into any conventional programming language +Host languages such as PL/1, COBOL, Pascal, C use the same style +Java solution looks a bit different +Dual-mode principle: every SQL statement can be used interactively as well as in an application program +All interactive SQL is embeddable, both DDL and DML, but in practice only DML gets embedded as DDL get run once only Some basics: +embedded SQL commands start with a specific symbol or key word. Examples are `$’ or `EXEC SQL’ +executable SQL statements can be used whenever executable programming language statements are allowed +SQL statements can include references to host variables (type compatibility is required)
4
6-4 CA306 Embedded SQL Application Program STEP 1: connect STEP 2: metadata query (optional) 1,000+ database tables STEP 3: execute SQL command
5
6-5 CA306 Embedded SQL Application Program STEP 4: query result set (optional) STEP 5: execute revised query (optional) 1,000+ database tables STEP 6: process result set results STEP 7: disconnect
6
6-6 CA306 Embedded SQL The Cursor (1) Example: $selectstatus, city into :st, :c fromS wheres# = ‘123’; +:st and :c are references to programming language variables (st and c) +similar programming language statements possible for SQL INSERT, DELETE, and UPDATE statements Problem: +some DML SELECT statements return a variable amount of data +host languages can’t (normally) deal with sets of tuples. Solution: +a cursor, a kind of pointer to process a set of tuples
7
6-7 CA306 Embedded SQL The Cursor (2) Active set and current row: +a set of tuples from a SELECT is called the active set +at any time we can only work with one tuple in the active set, called the current row A cursor is either open or closed +if open it points to one row +when closed a cursor does not have an active set +sequence of commands to create and use cursors: DECLARE OPEN FETCH CLOSE
8
6-8 CA306 Embedded SQL The Cursor - Operations DECLARE +names a cursor and the associated SQL SELECT statement +the SELECT statement may have programming language variables, thus making it dynamic +values for these parameters are computed at runtime OPEN +opens a cursor: runs the SELECT with the current program variable values +points to 1st row after execution FETCH +advances cursor to next row +retrieves values from that row +returns an error if something is wrong CLOSE +closes the cursor and releases the active set
9
6-9 CA306 Embedded SQL Cursor - Example 1 Example: $declare SN cursor for selectsname fromS wherecity=“Rome” ; $open SN do (* for all S-tuples accessible via SN *) $ fetch SN into :name end do ; $ close SN;
10
6-10 CA306 Embedded SQL Cursor - Example 2 #include char myCity[21]; -- stores values of DB attribute city:char(20) char name[21]; -- stores values of DB attribute sname:char(20) main() { $ declare SN cursor for select sname from S where city = :myCity; if (sqlcode==error) {.. terminate program.. } $ open SN; $ fetch SN into :name; while (sqlcode != error) { fprint(.. name.. );-- do something with it... $ fetch SN into :name; } $ close SN; }
11
6-11 CA306 Embedded SQL Other Issues Variables: +Host variables are prefixed with a colon ‘:’ +they must be associated with programming language data types, which are usually bigger (wider) to accommodate the possible NULL value SQL: char(n)Programming language: char(n+1) SQL: smallintProgramming language: integer SQL: date, timeProgramming language: structure +exact representation of NULL depends on the implementation +usually operations to test or set the NULL value are available Final remarks: +possible DB-development interactive testing implementation in programming language +compilation improves efficiency +disadvantage: SQL and host language are (normally) only loosely coupled.
12
6-12 CA306 Embedded SQL Transactions Transaction: +a set of operations which transform the database from one consistent state into another. Problem: a transaction might fail during execution SQL-support for transactions: +ROLLBACK: undo current transaction effects +COMMIT: confirm transaction effects Error handling can be described: $ whenever error-condition do command
13
6-13 CA306 Embedded SQL Sample Transaction BEGIN select * from A where A.x = select * from B where A.y = update A (column p = p * 0.1) update B (column q = q + 2) delete all tuples in C insert tuple(val,val,val,val) into C COMMIT / ROLLBACK
14
6-14 CA306 Embedded SQL Sections Covered 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases
15
6-15 CA306 Embedded SQL 6.2Java Database Connectivity Java DataBase Connectivity (JDBC) provides facilities to: connect to the database (Java class Connection) send an SQL statement to the database (Java class Statement) process a result (Java class ResultSet) through the java.sql API The difference to embedded SQL described in the previous section: An API is available, i.e. no special syntax necessary Location of the database system and form of connection is dealt with in the program The main conceptual problem which resulted in the introduction of cursors in embedded SQL for C, PL/I, Cobol etc. still exists The main concept to deal with the problem is the result set The class ResultSet offers a variety of ways to process a set of tuples - the result of a SELECT statement
16
6-16 CA306 Embedded SQL JDBC Basics Differences: pre-compilation is not necessary JDBC is Sun's solution to the inefficiency of CGI-scripts connecting to databases. The Java code is DBMS transparent, which means that any code needed to establish and maintain the connection to the database is hidden. JDBC drivers, called by methods of the Java classes Connection and Statement, handle the connection management. JDBC drivers for particular database management systems need to be installed, or a JDBC-ODBC bridge needs to be loaded if the connection to the database shall be made via Microsoft's ODBC mechanism. See Java JDBC API definition for more details.
17
6-17 CA306 Embedded SQL Sections Covered 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases
18
6-18 CA306 Embedded SQL 6.3Web Databases A simple Web-based architecture: +Client/server architecture +Information is stored in publicly accessible files on machines called Web servers +Files are encoded in HTML +Files are identified by URLs Data (files) is communicated using HTTP Applications: Web sites +online shopping (e-commerce, e-business) +virtual courses (e-learning) +public services (e-government)
19
6-19 CA306 Embedded SQL Web Architectures (1) A three-tiered architecture: Client (browser) – web server - backend (database) A Common Gateway Interface (CGI) may act as the middleware between a client and a database at the back end. CGI software executes programs/scripts to obtain dynamic information (instead of static file content)
20
6-20 CA306 Embedded SQL Web Architectures (2) Typical CGI languages: scripting: Perl, Tcl The main disadvantage of this approach is that for each user request the Web server starts a new process, which, in case of a database backend, then connects to the database. At the end of the request, the connection is closed and the process terminates. programs: Java (JDBC) JDBC (and Java servlets) should provide a more efficient platform, without the need for time-consuming additional processes and database connections.
21
6-21 CA306 Embedded SQL Databases in Web Architectures Database content can be displayed using a Web browser. The presentation can be formulated in HTML. Here is the table from a Supplier/Parts example: SNO SNAME STATUS CITY S1 Smith 20 Paris S2 Jones 10 Paris S3 Blake 30 Rome
22
6-22 CA306 Embedded SQL Representing Tables in HTML SNO SNAME STATUS CITY S1 Smith 20 Paris...... S2 Jones 10 Paris S3 Blake 30 Rome
23
6-23 CA306 Embedded SQL Databases and the Web - the Future Electronic Commerce: Database support is increasingly important for the emerging Electronic Commerce technologies Both business-to-consumer (B2C) and business-to business (B2B) eCommerce rely on data managed using database management systems, which can be accessed via the Internet. In the future, we expect to see: the convergence of Web and object technologies, e.g. the Document Object Model DOM, which allows us to see documents as objects. new languages more powerful than HTML, e.g. XML - the eXtensible Markup Language - allows us to define documents in a presentation-independent way and to exchange data independently of the system used to store the data. Both developments will have an effect on what kind of data is stored in the databases and how it is stored.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.