+ All Categories
Home > Documents > 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database...

1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database...

Date post: 25-Dec-2015
Category:
Upload: aron-parks
View: 249 times
Download: 0 times
Share this document with a friend
23
Section 6 Embedded SQL
Transcript
Page 1: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

1

Section 6

Embedded SQL

Page 2: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-2

Section Content

• 6.1 Embedded SQL• 6.2 Java Database Connectivity

• 6.3 Web Databases

Page 3: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-3

6.1 Embedded 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)

Page 4: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-4

ApplicationProgram

STEP 1: connect

STEP 2: metadata query (optional)

1,000+database tables

STEP 3: execute SQL command

Page 5: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-5

ApplicationProgram

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

Page 6: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-6

The Cursor (1)

• Example: $ select status, cityinto :st, :cfrom Swhere s# = ‘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

Page 7: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-7

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

Page 8: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-8

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

Page 9: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-9

Cursor - Example 1

• Example: $ declare SN cursor for

select snamefrom Swhere city=“Rome” ;

$ open SN do (* for all S-tuples accessible via SN *) $ fetch SN into :nameend do ;

$ close SN;

Page 10: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-10

Cursor - Example 2#include <stdio.h>

#include < .. other header files .. >

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;

}

Page 11: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-11

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: smallint Programming language: integer• SQL: date, time Programming 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.

Page 12: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-12

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

Page 13: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-13

Sample Transaction

BEGINselect * from A where A.x = <val>select * from B where A.y = <val>update A (column p = p * 0.1)update B (column q = q + 2)delete all tuples in Cinsert tuple(val,val,val,val) into C

COMMIT / ROLLBACK

Page 14: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-14

Sections Covered

6.1 Embedded SQL• 6.2 Java Database Connectivity

• 6.3 Web Databases

Page 15: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-15

6.2 Java 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

Page 16: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-16

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.

Page 17: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-17

Sections Covered

6.1 Embedded SQL 6.2 Java Database Connectivity

• 6.3 Web Databases

Page 18: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-18

6.3 Web 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)

Page 19: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-19

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)

Page 20: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-20

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.

Page 21: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-21

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

Page 22: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-22

Representing Tables in HTML

<table align=center border=2 cellpadding=2 bgcolor=white>

<tr bgcolor=grey>

<td>SNO</td>

<td>SNAME</td>

<td>STATUS</td>

<td>CITY</td>

</tr>

<tr>

<td>S1</td>

<td>Smith</td>

<td>20</td>

<td>Paris</td>

</tr>

...

...

<tr>

<td>S2</td>

<td>Jones</td>

<td>10</td>

<td>Paris</td>

</tr>

<tr>

<td>S3</td>

<td>Blake</td>

<td>30</td>

<td>Rome</td>

</tr>

</table>

Page 23: 1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.

CA306 Embedded SQL 6-23

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.


Recommended