2081 9

CSC419 ยท 2081 ยท 5 marks

What do you mean by SQL escape? Describe about scrollable and updateable result sets.

What do you mean by SQL escape? Describe about scrollable and updateable result sets. [5]

Advanced Java ProgrammingChapter 4: Database ConnectivityDDL and DML Operations using Java, Prepared Statements, Mult

Quick answer

SQL Escape (also called SQL escape syntax) is a standard JDBC mechanism that allows Java programs to use database-independent SQL syntax for features that differ across database vendors (such as date literals, stored procedure calls, outer joins, scalar functions, etc.).

SQL Escape and Scrollable/Updateable Result Sets


1. SQL Escape

JDBC translates the escape syntax into the database-specific SQL before sending the query, making the code portable across different databases.

Common SQL Escape Syntax:

PurposeEscape Syntax Example
Date literal{d 'yyyy-mm-dd'}
Time literal{t 'hh:mm:ss'}
Timestamp{ts 'yyyy-mm-dd hh:mm:ss'}
Stored procedure call{call procedureName(params)}
Scalar function{fn functionName(args)}
Outer join{oj table1 LEFT OUTER JOIN table2 ON ...}

Example:

String sql = "SELECT * FROM orders WHERE order_date = {d '2024-01-15'}";
PreparedStatement ps = con.prepareStatement(sql);

Here {d '2024-01-15'} is the escape sequence that JDBC translates to the vendor-specific date format automatically.


2. Scrollable Result Sets

By default, a ResultSet in JDBC is forward-only (can only move forward using next()). A Scrollable Result Set allows the cursor to move in both directions and jump to any row.

Types of Scrollable Result Sets:

TypeConstantDescription
Forward OnlyTYPE_FORWARD_ONLYDefault; cursor moves forward only
Scroll InsensitiveTYPE_SCROLL_INSENSITIVEScrollable; does NOT reflect changes made to DB
Scroll SensitiveTYPE_SCROLL_SENSITIVEScrollable; reflects changes made to DB
rs.next();          // Move to next row
rs.previous();      // Move to previous row
rs.first();         // Move to first row
rs.last();          // Move to last row
rs.absolute(n);     // Move to nth row
rs.relative(n);     // Move n rows from current position

Creating a Scrollable Result Set:

Statement stmt = con.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = stmt.executeQuery("SELECT * FROM students");

3. Updateable Result Sets

An Updateable Result Set allows the program to insert, update, or delete rows directly through the ResultSet object without writing separate SQL UPDATE/DELETE statements.

Concurrency Modes:

ModeConstantDescription
Read OnlyCONCUR_READ_ONLYDefault; cannot update through ResultSet
UpdatableCONCUR_UPDATABLEAllows updates through ResultSet

Common Update Methods:

rs.updateString("name", "New Name");   // Update a column value
rs.updateRow();                         // Commit the update to DB
rs.deleteRow();                         // Delete current row
rs.moveToInsertRow();                   // Move to insert buffer
rs.insertRow();                         // Insert the new row

4. Scrollable AND Updateable Result Set (Combined)

As stated in the notes, to create a scrollable insensitive result set which is also updatable, the statement must be created with both scroll type and updatable concurrency mode:

Statement stmt = con.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE,   // Scrollable
    ResultSet.CONCUR_UPDATABLE           // Updatable
);
ResultSet rs = stmt.executeQuery("SELECT * FROM students");

// Scroll to row 3 and update
rs.absolute(3);
rs.updateString("name", "Ram Sharma");
rs.updateRow();  // Changes saved to database

Summary Table:

FeatureDefault RSScrollable RSUpdateable RSScrollable + Updateable
Forward onlyYesNoYesNo
BidirectionalNoYesNoYes
Update rowsNoNoYesYes

Key Point: A scrollable updatable result set maintains a cursor that can both scroll (move in any direction) and update rows directly in the underlying database table.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.