0% found this document useful (0 votes)
5 views

8.SQLJ

The document outlines a course on Database Management Systems, focusing on foundational concepts, SQL programming, and application design. It details course objectives, outcomes, prerequisites, and provides information on SQLJ, a static model for embedding SQL in Java applications. Additionally, it includes references and links to further resources for self-assessment and innovative content.

Uploaded by

bhuvaneshwaricse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

8.SQLJ

The document outlines a course on Database Management Systems, focusing on foundational concepts, SQL programming, and application design. It details course objectives, outcomes, prerequisites, and provides information on SQLJ, a static model for embedding SQL in Java applications. Additionally, it includes references and links to further resources for self-assessment and innovative content.

Uploaded by

bhuvaneshwaricse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Database Management System

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

C303.1 Identify, analyze and define database objects, enforce integrity


constraints on a database using RDBMS.
C303.2 Use Structured Query Language (SQL) for database manipulation.

C303.3 Design and build simple database systems

C303.4 Apply the concepts of Normalization and design database which


possess no anomalies.
C303.5 Develop application to interact with databases

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

Students Should have the prior knowledge in


1. Basics of Java Programming language
2. JDBC Architecture
3. JDBC Classes and Interfaces

4
SQLJ

• SQLJ (short for 'SQL-Java') was developed by the SQLJ


Group, a group of database vendors and Sun.
• SQLJ was developed to complement the dynamic way of
creating queries in JDBC with a static model.
• It is therefore very close to Embedded SQL

5
SQLJ

• Unlike JDBC, having semi-static SQL queries allows the


compiler
• to perform SQL syntax checks,
• strong type checks of the compatibility of the host variables
with the respective SQL attributes,
• and consistency of the query with the database schema-tables,
attributes, views, and stored procedures--all at compilation
time.

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

String title; Float price; String author;


#sql iterator Books (String title, Float price);
Books books;
/ / the application sets the author
/ / execute the query and open the cursor
#sql books = {
SELECT title, price INTO :titIe, :price
FROM Books WHERE author = :author
};
/ / retrieve results
while (books.next()) {
System.out.println(books.titleO + ", " + books.price());
}
books.close() ;

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

Which of the following is used to call stored procedure?


a) Statement
b) PreparedStatement
c) CallableStatment
d) CalledStatement

19
Questions

1.What is SQLJ and how is it different from JDBC?(RBT:L2)


2.Why do we need a precompiler to translate embedded SQL and
SQL.J? Why do we not need a precompiler for JDBC? ?
(RBT:L2)
3. SQL.J and embedded SQL use variables in the host language
to pass parameters to SQL queries, whereas .JDBC uses
placeholders marked with a ''1'. Explain the difference, and
why the different mechanisms are needed. ?(RBT:L3)

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

You might also like