Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(BuildEntertainmentDB.java) { { import java.sql.*; /** * This program shows how

ID: 3572924 • Letter: #

Question

(BuildEntertainmentDB.java) { { import java.sql.*; /** * This program shows how to create a new database * using Java DB. */ public static void main(String(] args) throws Exception public class BuildEntertainmentDB final String DB_URL = "jdbc:derby:EntertainmentDB;create=true"; try { // Create a connection to the database. Connection conn = DriverManager.getConnection(DB_URL); // Create a Statement object. Statement stmt = conn.createStatement(); // Create the Dvd table. System.out.println("Creating the Dvd table..."); stmt.execute("CREATE TABLE Dvd (" + "Title CHAR(25), " + "Minutes INTEGER, " + "Price DOUBLE)"); // Close the resources, stmt.close(); conn.close(); System.out.println("Done"); } catch(Exception ex) { System.out.println("ERROR: M + ex.getMessage()); } When this program runs, the Entertainment DB database will be created. This is because the database URL, in line 13, has the ;create=true attribute. Lines 27 through 30 then create a table named Dvd. Scrollable Result Sets CONCEPT: A scrollable result set allows random cursor movement. By default, a result set is not scrollable. By default, ResultSet objects allow you to move the cursor forward only. Once the cursor has moved past a row, you cannot move the cursor backward to read that row again. If you need to move the cursor backward through the result set, you can create a scrollable result set. You do this when you create a Statement object by using an overloaded version of a Connection object's createStatement method. The method accepts two arguments. The first specifies the result set's scrolling type. You can use any of the following constants for this argument: ResultSet.TYPE_F0RWARD_ONLY This is the default scrolling type. It specifies that the result set's cursor should move only forward. ResultSet.TYPE_SCROLL_INSENSITIVE This specifies that the result set should be scrollable, allowing the cursor to move forward and backward through the result set. In addition, this result set is insensitive to changes } }

Explanation / Answer

import java.sql.*;
class ScrollableResultsetDemo
{
public static void main(String[] args)
{

try{
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
//to create scrollable resultset

Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

ResultSet rs=stmt.executeQuery("select * from DVD");

//reading the records from bottom to top
rs.afterLast();
while(rs.previous())
{
System.out.println(rs.getString(1)+" "+rs.getInt(2)+" "+rs.getDouble(3));
}

//move the cursor to 3rd record
rs.absolute(3);

System.out.println(rs.getString(1)+" "+rs.getInt(2)+" "+rs.getDouble(3));
//move the cursor to 2nd record using relative()
rs.relative(-1);
System.out.println(rs.getString(1)+" "+rs.getInt(2)+" "+rs.getDouble(3));
int position=rs.getRow(); // get cursor position
System.out.println("cursor position="+position);
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
//cleanup
rs.close();
stmt.close();
con.close();
}
}
}

output

dvd 50 25.65

james 23 30.6