Module 03
Module 03
3. What is SQLJ? How it is different from JDBC. Write a short note on Drivers in JDBC (8+4)
Answer:- SQLJ is a technology that embeds SQL queries in Java programs. It allows developers to
write SQL queries directly in Java code using special SQLJ syntax, which is then preprocessed by a
SQLJ translator to generate standard Java code that interacts with the database using JDBC.
The main difference between SQLJ and JDBC is in how SQL queries are handled. With JDBC, SQL
queries are typically written as strings within Java code, which can lead to potential syntax errors
and lack of type safety. In contrast, SQLJ provides a more integrated approach, allowing developers
to write SQL queries directly in Java code, making the code more readable, maintainable, and less
error-prone.
comparison of SQLJ and JDBC in tabular form:
Embeds SQL queries in Java code using SQL queries are written as strings within
Syntax
SQLJ syntax Java code
Provides better type safety as queries are Less type safety as queries are
Type Safety
checked at compile-time represented as strings
May offer better performance due to Performance can vary depending on query
Performance
precompiled and optimized queries implementation
1. Declaration: Cursors are first declared within the embedded SQL code block. This declaration
specifies the SQL query that will be associated with the cursor.
For example: EXEC SQL DECLARE cursor_name CURSOR FOR SELECT column1, column2 FROM
table_name WHERE condition;
2. Opening: Once declared, the cursor needs to be opened before fetching any rows from the
result set. This step establishes the connection between the cursor and the result set produced
by the SQL query.
For example: EXEC SQL OPEN cursor_name;
3. Fetching: After opening the cursor, rows from the result set can be fetched one at a time. The
fetched rows are typically stored in variables or data structures within the procedural
programming language for further processing.
For example: EXEC SQL FETCH NEXT FROM cursor_name INTO :variable1, :variable2;
This statement fetches the next row from the result set associated with cursor_name and stores
the values of column1 and column2 into variable1 and variable2, respectively.
4. Processing: Once a row is fetched, the procedural code can perform operations on the data
stored in the variables. This might involve computations, comparisons, or any other relevant
actions.
5. Looping: Steps 3 and 4 are typically performed in a loop until all rows have been fetched. This
loop continues until there are no more rows to fetch from the result set.
6. Closing: After all rows have been processed, the cursor should be closed to release associated
resources.
For example: EXEC SQL CLOSE cursor_name;
Using cursors in embedded SQL allows developers to manipulate result sets row by row, which can
be useful for tasks such as data processing, reporting, or batch operations. However, it's important
to use cursors judiciously, as they can sometimes lead to performance issues, especially when
dealing with large result sets.
7. Write a note on Specifying Constraints as Assertions in SQL. Also explain how assertions and
triggers are defined with an example.
8. What are stored procedures in SQL? Elaborate.
33