DBMS Module 3
DBMS Module 3
Module 3
CHAPTER 1: SQL: Advances Queries: More complex SQL retrieval queries, Specifying
constraints as assertions and action triggers, Views in SQL, Schema change statements in SQL.
CHAPTER 1
• In general, each individual NULL value is considered to be different from every other
NULL value in the various database records.
• When a record with NULL in one of its attributes is involved in a comparison operation,
the result is considered to beUNKNOWN (it may be TRUE or it may be FALSE).
• Hence, SQL uses a three-valued logic with values TRUE, FALSE, and UNKNOWN
instead of the standard two-valued (Boolean) logic with values TRUE or FALSE.
• It is therefore necessary to define the results (or truth values) of three-valued logical
expressions when the logical connectives AND, OR, and NOT are used.
• Some queries require that existing values in the database be fetched and then used in
a comparison condition. Such queries can be conveniently formulated by using
nested queries, which are complete select-from-where blocks within another SQL
query. That other query is called the outer query.
• These nested queries can also appear in the WHERE clause or the FROM clause or the
SELECT clause or other SQL clauses as needed.
• The comparison operator IN, which compares a value v with a set (or multiset) of
values V and evaluates to TRUE if v is one of the elements in V.
• If a nested query returns a single attribute and a single tuple, the query result will be
a single (scalar) value.
• In such cases, it is permissible to use = instead of IN for the comparison operator.
• In general, the nested query will return a table (relation), which is a set or multiset
of tuples.
• Example 1: selects the project numbers of projects that have an employee with last
name ‘Smith’ involved as manager or as a employee
• SQL allows the use of tuples of values in comparisons by placing them within
parentheses.
• Example 2: select the Essns of all employees who work the same (project, hours)
combination on some project that employee ‘John Smith’ (whose Ssn = ‘123456789’)
works on.
• Example 3: Find names of employees whose salary is greater than the salary of all
the employees in department 5:
SELECT Lname, Fname
FROM EMPLOYEE
WHERE Salary > ALL ( SELECT Salary
FROM EMPLOYEE
WHERE Dno = 5 );
• Example 4:Retrieve the name of each employee who has a dependent with the same
first name and is the same sex as the employee.
Whenever a condition in the WHERE clause of a nested query references some attribute
of a relation declared in the outer query, the two queries are said to be correlated.
• Example 4: Retrieve the name of each employee who has a dependent with the
same first name and is the same sex as the employee.
• Example 4: Retrieve the name of each employee who has a dependent with the
same first name and is the same sex as the employee.
• Example 6: List the names of managers who have at least one dependent.
• Example 7: Retrieve the name of each employee who works on all the projects
controlled by department number 5
EXPLICIT SETS
• It is also possible to use an explicit set of values in the WHERE clause, rather than a
nested query.
• Example 8: Retrieve the Social Security numbers of all employees who work on
project numbers 1, 2, or 3.
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE Pno IN (1, 2, 3);
RENAMING IN SQL
• In SQL, it is possible to rename any attribute that appears in the result of a query by
adding the qualifier AS followed by the desired new name.
• Hence, the AS construct can be used to alias both attribute and relation names in
general, and it can be used in appropriate parts of a query.
• Example 9: find the details of employee and their immediate supervisor
• The concept of a joined table (or joined relation) was incorporated into SQL to
permit users to specify a table resulting from a join operation in the FROM clause of a
query.
• This construct may be easier to comprehend than mixing together all the select and
join conditions in the WHERE clause.
• Example1: retrieves the name and address of every employee who works for the
‘Research’ department
• The concept of a joined table also allows the user to specify different types of join,
such as NATURAL JOIN and various types of OUTER JOIN.
• In a NATURAL JOIN on two relations R and S, no join condition is specified; an implicit
EQUIJOIN condition for each pair of attributes with the same name from R and S is
created.
• Each such pair of attributes is included only once in the resulting relation.
• If the names of the join attributes are not the same in the base relations, it is possible
to rename the attributes so that they match, and then to apply NATURAL JOIN.
• In this case, the AS construct can be used to rename a relation and all its attributes
in the FROM clause.
• The default type of join in a joined table is called an inner join, where a tuple is
included in the result only if a matching tuple exists in the other relation.
• It is also possible to nest join specifications; that is, one of the tables in a join may itself
be a joined table.
• This allows the specification of the join of three or more tables as a single joined table,
which is called a multiway join.
• Not all SQL implementations have implemented the new syntax of joined tables.
• In some systems, a different syntax was used to specify outer joins by using the
comparison operators + =, = +, and + = + for left, right, and full outer join,
respectively, when specifying the join condition.
• Aggregate functions are used to summarize information from multiple tuples into a
single-tuple summary.
• Grouping is used to create subgroups of tuples before summarization. Grouping and
aggregation are required in many database applications, and we will introduce their
use in SQL through examples.
• A number of built-in aggregate functions exist: COUNT, SUM, MAX, MIN, and AVG.
• The COUNT function returns the number of tuples or values as specified in a query.
• The functions SUM, MAX, MIN, and AVG can be applied to a set or multiset of numeric
values and return, respectively, the sum, maximum value, minimum value, and
average (mean) of those values.
• These functions can be used in the SELECT clause or in a HAVING clause.
• Example10: Find the sum of the salaries of all employees, the maximum salary, the
minimum salary, and the average salary.
• We could use AS to rename the column names in the resulting single-row table; for
example, as in example 10.
SELECT SUM (Salary) AS Total_Sal, MAX (Salary) AS Highest_Sal,
MIN (Salary) AS Lowest_Sal, AVG (Salary) AS Average_Sal
FROM EMPLOYEE;
• Example11: Find the sum of the salaries of all employees of the ‘Research’
department, as well as the maximum salary, the minimum salary, and the average
salary in this department.
• Example14: retrieve the names of all employees who have two or more
dependents.
• Note: If NULLs exist in the grouping attribute, then a separate group is created for
all tuples with a NULL value in the grouping attribute.
• Example 16: For each project, retrieve the project number, the project name, and
the number of employees who work on that project.
• Sometimes we want to retrieve the values of these functions only for groups that
satisfy certain conditions. SQL provides a HAVING clause, which can appear in
conjunction with a GROUP BY clause, for this purpose.
• HAVING provides a condition on the summary information regarding the group of
tuples associated with each value of the grouping attributes. Only the groups that
satisfy the condition are retrieved in the result of the query.
• Example 17: For each project on which more than two employees work, retrieve the
project number, the project name, and the number of employees who work on the
project.
• Example 18:For each project, retrieve the project number, the project name, and the
number of employees from department 5 who work on the project.
• Example 19: For each department that has more than five employees, retrieve the
department number and the number of its employees who are making more than
$40,000.
• The WITH clause allows a user to define a table that will only be used in a particular
query; it is somewhat similar to creating a view that will be used only in one query
and then dropped.
• Example:
• In the above example ,we defined in the WITH clause a temporary table BIG_DEPTS
whose result holds the Dno’s of departments with more than five employees, then
used this table in the subsequent query. Once this query is executed, the temporary
table BIGDEPTS is discarded.
• SQL also has a CASE construct, which can be used when a value can be different based
on certain conditions. This can be used in any part of an SQL query where a value is
expected, including when querying, inserting or updating tuples.
UPDATE EMPLOYEE
SET Salary =
CASE WHEN Dno = 5 THEN Salary + 2000
WHEN Dno = 4 THEN Salary + 1500
WHEN Dno = 1 THEN Salary + 3000
ELSE Salary + 0;
• The CASE construct can also be used when inserting tuples that can have different
attributes being NULL depending on the type of record being inserted into a table, as
when a specialization is mapped into a single table or when a union type is mapped
into relations.
SUMMARY:
SELECT <attribute and function list>
FROM <table list>
[ WHERE <condition> ]
[ GROUP BY <grouping attribute(s)> ]
[ HAVING <group condition> ]
[ ORDER BY <attribute list> ];
• In SQL, users can specify general constraints via declarative assertions, using the
CREATE ASSERTION statement.
• Each assertion is given a constraint name and is specified via a condition similar to
the WHERE clause of an SQL query.
• Example, specify the constraint that the salary of an employee must not be greater
than the salary of the manager of the department that the employee works for in SQL.
• The basic technique for writing such assertions is to specify a query that selects any
tuples that violate the desired condition.
• By including this query inside a NOT EXISTS clause, the assertion will specify that the
result of this query must be empty so that the condition will always be TRUE.
• Thus, the assertion is violated if the result of the query is not empty.
• In the preceding example, the query selects all employees whose salaries are greater
than the salary of the manager of their department. If the result of the query is not
empty, the assertion is violated.
• A major difference between CREATE ASSERTION and the individual domain
constraints and tuple constraints is that = the CHECK clauses on individual attributes,
domains, and tuples are checked in SQL only when tuples are inserted or updated in a
specific table.
• Hence, constraint checking can be implemented more efficiently by the DBMS in these
cases.
• The schema designer should use CHECK on attributes, domains, and tuples only when
he or she is sure that the constraint can only be violated by insertion or updating of
tuples.
• On the other hand, the schema designer should use CREATE ASSERTION only in cases
where it is not possible to use CHECK on attributes, domains, or tuples, so that simple
checks are implemented more efficiently by the DBMS.
Actions as Triggers
• In many cases it is convenient to specify the type of action to be taken when certain
events occur and when certain conditions are satisfied.
• The CREATE TRIGGER statement is used to implement such actions in SQL.
• A typical trigger which is regarded as an ECA (Event, Condition, Action) rule has three
components:
a. The event(s):
▪ These are usually database update operations that are explicitly applied to the
database.
▪ In this example the events are: inserting a new employee record, changing an
employee’s salary, or changing an employee’s supervisor.
▪ The person who writes the trigger must make sure that all possible events are
accounted for.
▪ In some cases, it may be necessary to write more than one trigger to cover all
possible cases.
▪ These events are specified after the keyword BEFORE in our example, which
means that the trigger should be executed before the triggering operation is
executed.
▪ An alternative is to use the keyword AFTER, which specifies that the trigger
should be executed after the operation specified in the event is completed.
b. The condition that determines whether the rule action should be executed:
▪ Once the triggering event has occurred, an optional condition may be
evaluated.
▪ If no condition is specified, the action will be executed once the event occurs.
If a condition is specified, it is first evaluated, and only if it evaluates to true
will the rule action be executed. The condition is specified in the WHEN clause
of the trigger.
• A view in SQL terminology is a single table that is derived from other tables.
• These other tables can be base tables or previously defined views.
• A view does not necessarily exist in physical form; it is considered to be a virtual
table, in contrast to base tables, whose tuples are always physically stored in the
database.
• This limits the possible update operations that can be applied to views, but it does not
provide any limitations on querying a view.
• In SQL, the command to specify a view is CREATE VIEW.
• The view is given a (virtual) table name (or view name), a list of attribute names, and
a query to specify the contents of the view. If none of the view attributes results from
applying functions or arithmetic operations, we do not have to specify new attribute
names for the view, since they would be the same as the names of the attributes of the
defining tables in the default case.
• If we do not need a view anymore, we can use the DROP VIEW command to dispose
of it.
a. Query modification:
• It involves modifying or transforming the view query into a query on the
underlying base tables.
• The disadvantage of this approach is that it is inefficient for views defined via
complex queries that are time-consuming to execute, especially if multiple
view queries are going to be applied to the same view within a short period of
time.
b. view materialization:
• It involves physically creating a temporary or permanent view table when the
view is first queried or created and keeping that table on the assumption that
other queries on the view will follow.
• In this case, an efficient strategy for automatically updating the view table
when the base tables are updated must be developed in order to keep the view
up-to-date.
• Techniques using the concept of incremental update have been developed
for this purpose, where the DBMS can determine what new tuples must be
inserted, deleted, or modified in a materialized view table when a database
update is applied to one of the defining base tables.
• The view is generally kept as a materialized (physically stored) table as long
as it is being queried.
• If the view is not queried for a certain period of time, the system may then
automatically remove the physical table and recompute it from scratch when
future queries reference the view.
• Different strategies as to when a materialized view is updated are possible.
• Theimmediate update strategy updates a view as soon as the base tables are
changed; the lazy update strategy updates the view when needed by a view
query; and the periodic update strategy updates the view periodically.
Example: UPDATE WORKS_ON1
SET Pname = ‘ProductY’
WHERE Lname = ‘Smith’ AND Fname = ‘John’ AND Pname = ‘ProductX’;
This query can be mapped into several updates on the base relations to give the desired
update effect on the view. In addition, some of these updates will create additional side
effects that affect the result of other queries. For example, here are two possible updates, (a)
and (b), on the base relations corresponding to the view update operation.
a. UPDATE WORKS_ON
SET Pno = ( SELECT Pnumber
FROM PROJECT
WHERE Pname = ‘ProductY’ )
WHERE Essn IN ( SELECT Ssn
FROM EMPLOYEE
WHERE Lname = ‘Smith’ AND Fname = ‘John’ )
AND
Pno = ( SELECT Pnumber
FROM PROJECT
WHERE Pname = ‘ProductX’ );
b. UPDATE PROJECT
SET Pname = ‘ProductY’
WHERE Pname = ‘ProductX’;
observations:
a. A view with a single defining table is updatable if the view attributes contain the
primary key of the base relation, as well as all attributes with the NOT NULL
constraint that do not have default values specified.
b. Views defined on multiple tables using joins are generally not updatable.
c. Views defined using grouping and aggregate functions are not updatable.
CHAPTER 2
Impedance Mismatch
• Impedance mismatch is the term used to refer to the problems that occur because
of differences between the database model and the programming language model.
• For example, the practical relational model has three main constructs: columns
(attributes) and their data types, rows (also referred to as tuples or records), and
tables (sets or multisets of records).
• The first problem that may occur is that the data types of the programming
language differ from the attribute data types that are available in the data model.
• Hence, it is necessary to have a binding for each host programming language that
specifies for each attribute type the compatible programming language types.
• A different binding is needed for each programming language because different
languages have different data types.
• Second problem occurs because the results of most queries are sets or multisets
of tuples (rows), and each tuple is formed of a sequence of attribute values.
• In the program, it is often necessary to access the individual data values within
individual tuples for printing or processing.
• Hence, a binding is needed to map the query result data structure, which is a table,
to an appropriate data structure in the programming language.
• A mechanism is needed to loop over the tuples in a query result in order to access a
single tuple at a time and to extract individual values from the tuple.
• The extracted attribute values are typically copied to appropriate program variables
for further processing by the program.
• A cursor or iterator variable is typically used to loop over the tuples in a query
result.
• Individual values within each tuple are then extracted into distinct program variables
of the appropriate type.
• Within an embedded SQL command, the programmer can refer to specially declared
C program variables; these are called shared variables because they are used in both
the C program and the embedded SQL statements.
• Shared variables are prefixed by a colon (:) when they appear in an SQL statement.
This distinguishes program variable names from the names of database schema
constructs such as attributes (column names) and relations (table names).
• It also allows program variables to have the same names as attribute names, since
they are distinguishable by the colon (:) prefix in the SQL statement.
• Names of database schema constructs—such as attributes and relations—can only be
used within the SQL commands, but shared program variables can be used elsewhere
in the C program without the colon (:) prefix.
• DECLARING VARIABLES
0) int loop ;
1) EXEC SQL BEGIN DECLARE SECTION ;
2) varchar dname [16], fname [16], lname [16], address [31] ;
3) char ssn [10], bdate [11], sex [2], minit [2] ;
4) float salary, raise ;
5) int dno, dnumber ;
6) int SQLCODE ; char SQLSTATE [6] ;
7) EXEC SQL END DECLARE SECTION;
• A few of the common bindings of C types to SQL types are as follows. The SQL types
INTEGER, SMALLINT, REAL, and DOUBLE are mapped to the C data types long, short,
float, and double, respectively.
• Fixed-length and varying-length strings (CHAR [i], VARCHAR [i]) in SQL can be
mapped to arrays of characters (char [i+1], varchar [i+1]) in C that are one character
longer than the SQL type because strings in C are terminated by a NULL character
(\0), which is not part of the character string itself.
• Although varchar is not a standard C data type, it is permitted when C is used for SQL
database programming.
The programmer or user can use the <connection name> to change from the currently
active connection to a different one by using the following command:
• The two special communication variables that are used by the DBMS to
communicate exception or error conditions to the program are SQLCODE and
SQLSTATE.
• The SQLCODE variable is an integer variable.
• After each database command is executed, the DBMS returns a value in SQLCODE.
• A value of 0 indicates that the statement was executed successfully by the DBMS.
• If SQLCODE > 0 (or, more specifically, if SQLCODE = 100), this indicates that no more
data (records) are available in a query result. If SQLCODE < 0, this indicates some
error has occurred.
• In some systems—for example, in the Oracle RDBMS—SQLCODE is a field in a record
structure called SQLCA (SQL communication area), so it is referenced as
SQLCA.SQLCODE.
• In this case, the definition of SQLCA must be included in the C program by including
the following line:
EXEC SQL include SQLCA ;
• In later versions of the SQL standard, a communication variable called SQLSTATE
was added, which is a string of five characters.
• A value of ‘00000’ in SQLSTATE indicates no error or exception; other values
indicate various errors or exceptions.
• For example, ‘02000’ indicates ‘no more data’ when using SQLSTATE.
• Currently, both SQLSTATE and SQLCODE are available in the SQL standard
0) loop = 1 ;
1) while (loop) {
2) prompt("Enter a Social Security Number: ", ssn) ;
3) EXEC SQL
4) SELECT Fname, Minit, Lname, Address, Salary
5) INTO :fname, :minit, :lname, :address, :salary
6) FROM EMPLOYEE WHERE Ssn = :ssn ;
7) if (SQLCODE = = 0) printf(fname, minit, lname, address, salary)
8) else printf("Social Security Number does not exist: ", ssn) ;
9) prompt("More Social Security Numbers (enter 1 for Yes, 0 for No): ", loop) ;
10) }
• The INTO clause specifies the program variables into which attribute values from the
database record are retrieved. C program variables in the INTO clause are prefixed
with a colon (:), as we discussed earlier.
• The INTO clause can be used in this manner only when the query result is a single
record; if multiple records are retrieved, an error will be generated.
• A cursor is a variable that refers to a single tuple (row) from a query result that
retrieves a collection of tuples. It is used to loop over the query result, one record at
a time. The cursor is declared when the SQL query is declared.
• The default is that the query is for retrieval purposes (FOR READ ONLY).
• If some of the tuples in the query result are to be updated, we need to specify FOR
UPDATE OF <attribute list> and list the attributes that may be updated.
• If some tuples are to be deleted, we need to specify FOR UPDATE without any
attributes listed.
• When the optional keyword SCROLL is specified in a cursor declaration, it is possible
to position the cursor in other ways than for purely sequential access.
• A fetch orientation can be added to the FETCH command, whose value can be one of
NEXT, PRIOR, FIRST, LAST, ABSOLUTE i, and RELATIVE i.
• When SCROLL is specified on the cursor, the general form of a FETCH command is as
follows, with the parts in square brackets being optional:
FETCH [ [ <fetch orientation> ] FROM ] <cursor name> INTO <fetch target list>;
• An OPEN CURSOR command fetches the query result from the database and sets the
cursor to a position before the first row in the result of the query. This becomes the
current row for the cursor.
• Subsequently, FETCH commands are issued in the program; each FETCH moves the
cursor to the next row in the result of the query, making it the current row and copying
its attribute values into the C (host language) program variables specified in the
FETCH command by an INTO clause.
• The cursor variable is basically an iterator that iterates (loops) over the tuples in the
query result—one tuple at a time.
• A CLOSE CURSOR command is issued to indicate that we are done with processing
the result of the query associated with that cursor.
• In some cases, it is convenient to write a program that can execute different SQL
queries or updates (or other operations) dynamically at runtime.
• Program segment reads a string that is input by the user (that string should be an SQL
update command in this example) into the string program variable sqlupdatestring in
line 3.
• It then prepares this as an SQL command in line 4 by associating it with the SQL
variable sqlcommand.
• Line 5 then executes the command. Notice that in this case no syntax check or other
types of checks on the command are possible at compile time, since the SQL command
is not available until runtime.
• This contrasts with our previous examples of embedded SQL, where the query could
be checked at compile time because its text was in the program source code.
• In program segment, the reason for separating PREPARE and EXECUTE is that if
the command is to be executed multiple times in a program, it can be prepared only
once.
• Preparing the command generally involves syntax and other types of checks by the
system, as well as generating the code for executing it. It is possible to combine the
PREPARE and EXECUTE commands (lines 4 and 5 in program above) into a single
statement by writing
• SQLJ is a standard that has been adopted by several vendors for embedding SQL in
Java.
• Historically, SQLJ was developed after JDBC, which is used for accessing SQL
databases from Java using class libraries and function calls.
• An SQLJ translator will generally convert SQL statements into Java, which can then
be executed through the JDBC interface. Hence, it is necessary to install a JDBC driver
when using SQLJ.
• Before being able to process SQLJ with Java in Oracle, it is necessary to import
severalclass libraries.
• These include the JDBC and IO classes (lines 1 and 2), plus the additional classes listed
in lines 3, 4, and 5.
1) import java.sql.* ;
2) import java.io.* ;
3) import sqlj.runtime.* ;
4) import sqlj.runtime.ref.* ;
5) import oracle.sqlj.runtime.* ;
...
6) DefaultContext cntxt =
7) oracle.getConnection("<url name>", "<user name>", "<password>", true) ;
8) DefaultContext.setDefaultContext(cntxt) ;
...
In addition, the program must first connect to the desired database using the function call
getConnection, which is one of the methods of the oracle class in line 5.
• Notice that because Java already uses the concept of exceptions for error handling, a
special exception called SQLException is used to return errors or exception
conditions after executing an SQL database command.
• This plays a similar role to SQLCODE and SQLSTATE in embedded SQL.
• Java has many types of predefined exceptions. Each Java operation (function) must
specify the exceptions that can be thrown—that is, the exception conditions that may
occur while executing the Java code of that operation.
• If a defined exception occurs, the system transfers control to the Java code specified
for exception handling.
Declaring variables:
Java program segment that uses a named iterator to print employee information in a
particular department.
• The positional iterator behaves in a manner that is more similar to embedded SQL.
• The first time fetch is executed, it gets the first tuple. To control the loop, a positional
iterator function e.endFetch() is used.
• This function is automatically set to a value of TRUE when the iterator is initially
associated with an SQL query (line 11), and is set to FALSE each time a fetch command
returns a valid tuple from the query result. It is set to TRUE again when a fetch
command does not find any more tuples.
A Java program segment that uses a positional iterator to print employee information
in a particular department.
DEPT OF CSE Page 29 of 45
SJCIT DBMS MODULE 3
• Stored procedures are program modules that are stored by the DBMS at the database
server.
• In Database programming techniques , there was an implicit assumption that the
database application program was running on a client machine, or more likely at the
application server computer in the middle-tier of a three-tier client-server
architecture .
• In either case, the machine where the program is executing is different from the
machine on which the database server—and the main part of the DBMS software
package—is located.
• Although this is suitable for many applications, it is sometimes useful to create
database program modules—procedures or functions—that are stored and executed
by the DBMS at the database server.
• These are historically known as database stored procedures, although they can be
functions or procedures.
• The term used in the SQL standard for stored procedures is persistent stored
modules because these programs are stored persistently by the DBMS, similarly to
the persistent data stored by the DBMS.
• The parameters and local declarations are optional, and are specified only if needed.
• For declaring a function, a return type is necessary, so the declaration form is:
CREATE FUNCTION <function name> (<parameters>)
RETURNS <return type>
<local declarations>
<function body> ;
• Because the procedures and functions are stored persistently by the DBMS, it should
be possible to call them from the various SQL interfaces and programming
techniques.
• The CALL statement in the SQL standard can be used to invoke a stored procedure—
either from an interactive interface or from embedded SQL or SQLJ.
• The format of the statement is as follows:
• SQL/PSM is the part of the SQL standard that specifies how to write persistent stored
modules.
• It includes the statements to create functions and procedures. It also includes
additional programming constructs to enhance the power of SQL for the purpose of
writing the code (or body) of stored procedures and functions.
• SQL/PSM has several constructs for looping. There are standard while and repeat
looping structures, which have the following forms:
WHILE <condition> DO
<statement list>
END WHILE ;
REPEAT
<statement list>
UNTIL <condition>
END REPEAT ;
• There is also a cursor-based looping structure. The statement list in such a loop is
executed once for each tuple in the query result.
• This has the following form:
FOR <loop name> AS <cursor name> CURSOR FOR <query> DO <statement list>
END FOR ;
• Loops can have names, and there is a LEAVE <loop name> statement to break a loop
when a condition is satisfied.
INTRODUCTION TO JDBC
• ODBC and JDBC, short for Open DataBase Connectivity and Java DataBase
Connectivity, also enable the integration of SQL with a general-purpose programming
language.
• Both ODBC and JDBC expose database capabilities in a standardized way to the
application programmer through an application programming interface (API).
• In contrast to Embedded SQL, ODBC and JDBC allow a single executable to access
different DBMSs 'Without recompilation.
• Thus, while Embedded SQL is DBMS-independent only at the source code level,
applications using ODBC or JDBC are DBMS-independent at the source code level and
at the level of the executable.
• In addition, using ODBC or JDBC, an application can access not just one DBMS but
several different ones simultaneously.
• ODBC and JDBC achieve portability at the level of the executable by introducing an
extra level of indirection.
• All direct interaction with a specific DBMS happens through a DBMS-specific driver.
A driver is a software program that translates the ODBC or JDBC calls into DBMS-
specific calls.
• Drivers are loaded dynamically on demand since the DBMSs the application is going
to access are known only at run-time. Available drivers are registered with a driver
manager.
• An application that interacts with a data source through ODBC or JDBC selects a data
source, dynamically loads the corresponding driver, and establishes a connection
with the data source.
• There is no limit on the number of open connections, and an application can have
several open connections to different data sources. Each connection has transaction
semantics; that is, changes from one connection are visible to other connections only
after the connection has committed its changes.
• While a connection is open, transactions are executed by submitting SQL statements,
retrieving results, processing errors, and finally committing or rolling back. The
application disconnects from the data source to terminate the interaction.
ARCHITECTURE OF JDBC
• The architecture of JDBC has four main components: the application, the driver
manager, several data source specific drivers, and the corresponding data sources.
a. The application initiates and terminates the connection with a data source. It sets
transaction boundaries, submits SQL statements, and retrieves the results-----all
through a well-defined interface as specified by the JDBC API.
b. The primary goal of the driver manager is to load JDBC drivers and pass JDBC
function calls from the application to the correct driver. The driver manager also
handles JDBC initialization and information calls from the applications and can log all
function calls. In addition, the driver manager performs· some rudimentary error
checking.
c. The driver establishes the connection with the data source. In addition to submitting
requests and returning request results, the driver translates data, error formats, and
error codes from a form that is specific to the data source into the JDBC standard.
d. The data source processes commands from the driver and returns the results.
JDBC driver implementations vary because of the wide variety of operating systems and
hardware platforms in which Java operates. Sun has divided the implementation types into
four categories, Types 1, 2, 3, and 4, which is explained below −
In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client
machine. Using ODBC, requires configuring on your system a Data Source Name (DSN) that
represents the target database.
When Java first came out, this was a useful driver because most databases only supported
ODBC access but now this type of driver is recommended only for experimental use or when
no other alternative is available.
The JDBC-ODBC Bridge that comes with JDK 1.2 is a good example of this kind of driver.
In a Type 2 driver, JDBC API calls are converted into native C/C++ API calls, which are
unique to the database. These drivers are typically provided by the database vendors and
used in the same manner as the JDBC-ODBC Bridge. The vendor-specific driver must be
installed on each client machine.
If we change the Database, we have to change the native API, as it is specific to a database
and they are mostly obsolete now, but you may realize some speed increase with a Type 2
driver, because it eliminates ODBC's overhead.
In a Type 3 driver, a three-tier approach is used to access databases. The JDBC clients use
standard network sockets to communicate with a middleware application server. The
socket information is then translated by the middleware application server into the call
format required by the DBMS, and forwarded to the database server.
This kind of driver is extremely flexible, since it requires no code installed on the client and
a single driver can actually provide access to multiple databases.
You can think of the application server as a JDBC "proxy," meaning that it makes calls for
the client application. As a result, you need some knowledge of the application server's
configuration in order to effectively use this driver type.
Your application server might use a Type 1, 2, or 4 driver to communicate with the database,
understanding the nuances will prove helpful.
In a Type 4 driver, a pure Java-based driver communicates directly with the vendor's
database through socket connection. This is the highest performance driver available for
the database and is usually provided by the vendor itself.
This kind of driver is extremely flexible, you don't need to install special software on the
client or server. Further, these drivers can be downloaded dynamically.
There are six steps involved in building a JDBC application which I'm going to brief in this
tutorial:
This requires that you include the packages containing the JDBC classes needed for database
programming. Most often, using import java.sql.* will suffice as follows:
This requires that you initialize a driver so you can open a communications channel with
the database. Following is the code snippet to achieve this:
Open a connection:
Execute a query:
This requires using an object of type Statement or PreparedStatement for building and
submitting an SQL statement to the database as follows:
If there is an SQL UPDATE, INSERT or DELETE statement required, then following code
snippet would be required:
This step is required in case you are fetching data from the database. You can use the
appropriate ResultSet.getXXX() method to retrieve the data from the result set as follows:
You should explicitly close all database resources versus relying on the JVM's garbage
collection as follows:
Example 1: Java program segment that uses JDBC for a query with a tuple in its result
0) import java.io.* ;
1) import java.sql.*
...
2) class getEmpInfo {
3) public static void main (String args []) throws SQLException, IOException {
4) try { Class.forName("oracle.jdbc.driver.OracleDriver")
5) } catch (ClassNotFoundException x) {
6) System.out.println ("Driver could not be loaded") ;
7) }
8) String dbacct, passwrd, ssn, lname ;
9) Double salary ;
10) dbacct = readentry("Enter database account:") ;
11) passwrd = readentry("Enter password:") ;
12) Connection conn = DriverManager.getConnection ("jdbc:oracle:oci8:" + dbacct + "/" +
passwrd) ;
14) String stmt1 = "select Lname, Salary from EMPLOYEE where Ssn = ?" ;
15) PreparedStatement p = conn.prepareStatement(stmt1) ;
16) ssn = readentry("Enter a Social Security Number: ") ;
17) p.clearParameters() ;
18) p.setString(1, ssn) ;
19) ResultSet r = p.executeQuery() ;
20) while (r.next()) {
21) lname = r.getString(1) ;
22) salary = r.getDouble(2) ;
23) system.out.printline(lname + salary) ;
24) } }
25) }
Example 2: Java program segment that uses JDBC for a query with a collection of
tuples in its result.
0) import java.io.* ;
1) import java.sql.*
...
2) class printDepartmentEmps {
3) public static void main (String args [])
throws SQLException, IOException {
4) try { Class.forName("oracle.jdbc.driver.OracleDriver")
5) } catch (ClassNotFoundException x) {
6) System.out.println ("Driver could not be loaded") ;
7) }
8) String dbacct, passwrd, lname ;
9) Double salary ;
10) Integer dno ;
11) dbacct = readentry("Enter database account:") ;
12) passwrd = readentry("Enter password:") ;
13) Connection conn = DriverManager.getConnection
14) ("jdbc:oracle:oci8:" + dbacct + "/" + passwrd) ;
15) dno = readentry("Enter a Department Number: ") ;
16) String q = "select Lname, Salary from EMPLOYEE where Dno = " +dno.tostring() ;
17) Statement s = conn.createStatement() ;
18) ResultSet r = s.executeQuery(q) ;
19) while (r.next( )) {
20) lname = r.getString(1) ;
21) salary = r.getDouble(2) ;
22) system.out.printline(lname + salary) ;
23) } }
24) }
2. Thin Clients: Clients only need enough computation power for the presentation layer.
Typically, clients are Web browsers.
3. Integrated Data Access: In many applications, the data must be accessed from
several sources. This can be handled transparently at the middle tier, where we can
centrally manage connections to all database systems involved.
4. Scalability to Many Clients: Each client is lightweight and all access to the system is
through the middle tier. The middle tier can share database connections across
clients, and if the middle tier becomes the bottle-neck, we can deploy several servers
executing the middle tier code; clients can connect to anyone of these servers, if the
logic is designed appropriately.
5. Software Development Benefits: By dividing the application cleanly into parts that
address presentation, data access, and business logic, we gain many advantages. The
business logic is centralized, and is therefore easy to maintain, debug, and change.
Interaction between tiers occurs through well-defined, standardized APls. Therefore,
each application tier can be built out of reusable components that can be individually
developed, debugged, and tested.
HTML Forms
• HTML forms are a common way of communicating data from the client tier to the
middle tier. The general format of a form is the following:
• A single HTML document can contain more than one form. Inside an HTML form, we
can have any HTML tags except another FORM element.
ii. METHOD: The HTTP/1.0 method used to submit the user input from the filled-
out form to the webserver. There are two choices, GET and POST;
iii. NAME: This attribute gives the form a name. Although not necessary, naming
forms is good style.
• Inside HTML forms, the INPUT, SELECT, and TEXTAREA tags are used to specify user
input elements; a form can have many elements of each type.
• The Common Gateway Interface connects HTML forms with application programs.
• It is a protocol that defines how arguments from forms are passed to programs at the
server side.
• Programs that communicate with the webserver via CGI are often called CGI scripts,
since many such application programs were written in a scripting language such like
Perl.
✓ Servlets
Java servlets are pieces of Java code that run on the middle tier, in either webservers or
application servers.
import java.io. *;
import javCLx.servlet. *;
import javax.servlet.http. *;
pUblic class ServletTemplate extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
✓ JavaServer Pages
• JavaServer pages (.JSPs) interchange the roles of output amI application logic.
• JavaServer pages are written in HTML with servlet-like code embedded in special
HT1VIL tags.
• Thus, in comparison to servlets, JavaServer pages are better suited to quickly building
interfaces that have some logic inside, whereas , servlets are better suited for complex
application logic.
import java.io. *;
import javax.servlet. *;
import javax.servlet.http. *;
import java.util.*;
public class ReadUserName extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType('j textjhtml'j);
PrintWriter out = response.getWriter();
out.println("<BODY>\n" +
"<Hi ALIGN=CENTER> Username: </Hi>\n" + "<UL>\n" + " <LI>title: "
+ request.getParameter("userid") + "\n" +
+ request.getParameter("password'j) + "\n”+ “</UL>\n" + 1</BODY></HTML>")”
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}