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

read the following program and describe what the program does. write comments in

ID: 3571589 • Letter: R

Question

read the following program and describe what the program does. write comments in the program listing to follow "//".

import java.io.*;

import java.sql.*;

public class Example { public static void main (String args []) throw SQL Exception, IOException {

try {

// //

Class.forName("oracle.jdbc.driver.OracleDriver");

}

// //

catch (ClassNotFoundException ex){

System.out.println("Driver could not be loaded");

return; }

// //

String url= jdbc:oracle:www.bookstore.com:3083";

// //

Connection con;

try {

// //

con= DriveManager.getConnection(url, userId, password);

// //

String sqlQuery = "SELECT isbn, title FROM Books WHERE publisher = 'MIT Press'"'

//

//

PreparedStatement stmt = con.prepareStatement(sqlQuery);

//

//

ResultSet rs = stmt.executeQuery(sqlQuery);

String isbn, title;

System.out.println("isbn, title");

//

//

while(rs.next()) {

isbn = rs.getString(1);

title = rs.getString("TITLE");

System.out.println(isbn + " " + title);

}

}

//

catch(SQLException ex){

System.out.println("ex.getMessage());

}

}

}

Explanation / Answer

import java.io.*;
import java.sql.*;
public class Example
{
   public static void main (String args []) throw SQL Exception, IOException
   {
try {
// this will returns the Class object of the name passed in the parameter
   Class.forName("oracle.jdbc.driver.OracleDriver");
}
// this will be executed if the class object is not returned
   catch (ClassNotFoundException ex)
   {
   System.out.println("Driver could not be loaded");
   return;
   }
   // // the below line will declare the string named with url which will tell the compiler the path of database
   String url= jdbc:oracle:www.bookstore.com:3083";
  
   // // the below line will declare the object of type connection which is use to make a conection with databas
   Connection con;
   try {
  
   // //the below line will create a connection by passing the url by using the userId and password
   con= DriveManager.getConnection(url, userId, password);
  
   // // this line will declare the SQL query which will return a table from the database
   String sqlQuery = "SELECT isbn, title FROM Books WHERE publisher = 'MIT Press'"'
  
// the below line will be used to execute the query which is passed in the parameter
   PreparedStatement stmt = con.prepareStatement(sqlQuery);
  
   // The ResultSet class object is use to execute the query and will store the result in rs named object`
   ResultSet rs = stmt.executeQuery(sqlQuery);
   String isbn, title;
   System.out.println("isbn, title");

   //the below code will loop over the object rs until the rs will have values
   while(rs.next()) {
   isbn = rs.getString(1);
   title = rs.getString("TITLE");
   System.out.println(isbn + " " + title);
   }
   }
   //   this will be exectued in case if any of the above lines throw any exception
   catch(SQLException ex){
   System.out.println("ex.getMessage());
   }
   }
   }