* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download eCommerce&Security - DCU School of Computing
Serializability wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Tandem Computers wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Access wikipedia , lookup
Relational algebra wikipedia , lookup
Ingres (database) wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
ContactPoint wikipedia , lookup
Clusterpoint wikipedia , lookup
Database model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Relational model wikipedia , lookup
Section 6
Embedded SQL
1
Section Content
• 6.1 Embedded SQL
• 6.2 Java Database Connectivity
• 6.3 Web Databases
CA306 Embedded SQL
6-2
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)
CA306 Embedded SQL
6-3
STEP 1: connect
Application
Program
STEP 2: metadata query (optional)
STEP 3: execute SQL command
1,000+
database tables
CA306 Embedded SQL
6-4
STEP 4: query result set (optional)
Application
Program
STEP 5: execute revised query (optional)
STEP 6: process result set
STEP 7: disconnect
results
CA306 Embedded SQL
1,000+
database tables
6-5
The Cursor (1)
• Example:
$ select
into
from
where
status, city
:st, :c
S
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
CA306 Embedded SQL
6-6
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:
•
•
•
•
CA306 Embedded SQL
DECLARE
OPEN
FETCH
CLOSE
6-7
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
CA306 Embedded SQL
6-8
Cursor - Example 1
• Example:
$ declare SN cursor for
select sname
from
S
where city=“Rome” ;
$ open SN
do (* for all S-tuples accessible via SN *)
$ fetch SN into :name
end do ;
$ close SN;
CA306 Embedded SQL
6-9
Cursor - Example 2
#include <stdio.h>
#include < .. other header files .. >
char myCity[21];
char name[21];
-- stores values of DB attribute city:char(20)
-- 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 .. );
$ fetch SN into :name;
}
-- do something with it ...
$ close SN;
}
CA306 Embedded SQL
6-10
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.
CA306 Embedded SQL
6-11
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
CA306 Embedded SQL
6-12
Sample Transaction
BEGIN
select * 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 C
insert tuple(val,val,val,val) into C
COMMIT / ROLLBACK
CA306 Embedded SQL
6-13
Sections Covered
6.1 Embedded SQL
• 6.2 Java Database Connectivity
• 6.3 Web Databases
CA306 Embedded SQL
6-14
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
CA306 Embedded SQL
6-15
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.
CA306 Embedded SQL
6-16
Sections Covered
6.1 Embedded SQL
6.2 Java Database Connectivity
• 6.3 Web Databases
CA306 Embedded SQL
6-17
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)
CA306 Embedded SQL
6-18
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)
CA306 Embedded SQL
6-19
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.
CA306 Embedded SQL
6-20
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:
CA306 Embedded SQL
SNO
SNAME
STATUS
CITY
S1
Smith
20
Paris
S2
Jones
10
Paris
S3
Blake
30
Rome
6-21
Representing Tables in HTML
<table align=center border=2
cellpadding=2 bgcolor=white>
<tr bgcolor=grey>
<td>SNO</td>
...
<tr>
<td>SNAME</td>
<td>S2</td>
<td>STATUS</td>
<td>Jones</td>
<td>CITY</td>
<td>10</td>
</tr>
<tr>
<td>S1</td>
<td>Paris</td>
</tr>
<tr>
<td>Smith</td>
<td>S3</td>
<td>20</td>
<td>Blake</td>
<td>Paris</td>
<td>30</td>
</tr>
...
<td>Rome</td>
</tr>
</table>
CA306 Embedded SQL
6-22
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.
CA306 Embedded SQL
6-23