8.SQLJ
8.SQLJ
18CS53
1
Course objectives: This course will enable students to
1. Provide a strong foundation in database concepts, technology, and
practice.
2. Practice SQL programming through a variety of database problems.
3. Demonstrate the use of concurrency and transactions in database
4. Design and build database applications for real world problems.
Course Outcome
Course Code Course Outcome
2
Textbooks:
1. Fundamentals of Database Systems, Ramez Elmasri and
Shamkant B. Navathe, 7th Edition, 2017,Pearson.
2. Database management systems, Ramakrishnan, and Gehrke,
3rd Edition, 2014, McGraw Hill
Reference Books:
1. Silberschatz Korth and Sudharshan, Database System
Concepts, 6th Edition, Mc-GrawHill, 2013.
2. Coronel, Morris, and Rob, Database Principles Fundamentals
of Design, Implementation and Management, Cengage
Learning 2012.
3
Prerequisite to learn SQLJ
4
SQLJ
5
SQLJ
6
• For example, in both SQLJ and Embedded SQL, variables in
the host language always are bound statically to the same
arguments, whereas in
• JDBC, we need separate statements to bind each variable to
an argument and to retrieve the result.
7
Figure : Oracle SQLJ
8
• For example, the following SQLJ statement binds host
language variables title, price, and author to the return values
of the cursor books.
#sql books = { SELECT title, price INTO :title, :price
FROM Books WHERE author = :author
};
9
• When writing SQLJ applications, we just write regular Java
code and embed SQL statements according to a set of rules.
• SQLJ applications are pre-processed through an SQLJ
translation program that replaces the embedded SQLJ code
with calls to an SQLJ Java library.
• The modified program code can then be compiled by any Java
compiler.
• Usually the SQLJ Java library makes calls to a JDBC driver,
which handles the connection to the database system.
10
Writing SQLJ Code
11
The corresponding JDBC code fragment looks as follows (assuming we
also declared price, name, and author:
PreparedStatcment stmt = connection.prepareStatement( " SELECT
title, price FROM Books WHERE author = ?");
/ / set the parameter in the query ancl execute it
stmt.setString(1, author);
ResultSet rs = stmt.executeQuery();
/ / retrieve the results
while (rs.next()) {
System.out.println(rs.getString(l) + ", " + rs.getFloat(2));
}
12
• Comparing the JDBC and SQLJ code, we see that the SQLJ
code is much easier to read than the JDBC code.
• Thus, SQLJ reduces software development and maintenance
costs.
13
• Let us consider the individual components of the SQLJ code in
more detail. All SQLJ statements have the special prefix #sql.
• In SQLJ, we retrieve the results of SQL queries with iterator
objects, which are basically cursors.
• An iterator is an instance of an iterator class. Usage of an
iterator in SQLJ goes through five steps
14
Declare the Iterator Class: In the preceding code, this
happened through the statement
#sql iterator Books (String title, Float price);
This statement creates a new Java class that we can use to
instantiate
objects.
• Instantiate an Iterator Object from the New Iterator Class: We
instantiated our iterator in the statement Books books;.
• Initialize the Iterator Using a SQL Statement: In our example, this
happens through the statement #sql books ;;;;;; ....
• Iteratively, Read the Rows From the Iterator Object: This step is
very similar to reading rows through a ResultSet object in JDBC.
• Close the Iterator Object.
15
• There are two types of iterator classes:
• named iterators and positional iterators.
• For named iterators, we specify both the variable type and
the name of each column of the iterator.
• This allows us to retrieve individual columns by name as in
our previous example where we could retrieve the title colunm
from the Books table using the expression books.titIe().
16
• For positional iterators, we need to specifY only the variable
type for each column of the iterator.
• To access the individual columns of the iterator, we use a
FETCH ... INTO construct, similar to Embedded SQL.
Both iterator types have the same performance;
which iterator to use depends on the programmer's taste.
17
The interface ResultSet has a method, getMetaData(), that
returns a/an
A. Tuple
B. Value
C. Object
D. Result
Which of the following contains both date and time?
a) java.io.date
b) java.sql.date
c) java.util.date
d) java.util.dateTime
18
• What does setAutoCommit (false) do?
a) commits transaction after each query
b) explicitly commits transaction
c) does not commit transaction automatically after each query
d) never commits transaction
19
Questions
20
FDP CONTENT LINK
https://nptel.ac.in/courses/106/105/106105175/ (AICTE Approved FDP link)
Self Assessment Quiz link
https://forms.gle/juPiZA1N6V2omoDD7
Innovative Content Link
1. https://www.youtube.com/watch?v=CAanqvvDsTw&list=PLEbnTDJUr_I
c_9b4PcKmlae41cyxEefot&index=11
(GATE TRAINER LINK DBMS)
2. https://www.youtube.com/watch?v=5GQbACGHY_E&list=PLL_LQvNX
4xKyiExzq9GKwORoH6nvaRnOQ&index=24
(PROFESSIONAL TRAINER FOR DBMS)
21
References
1. https://www.theserverside.com/definition/SQLJ
2. Database management systems, Ramakrishnan, and Gehrke,
3rd Edition, 2014, McGraw Hill
22