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

What should i write on bolded Question mark below ? Also tell me in which format

ID: 3717360 • Letter: W

Question

What should i write on bolded Question mark below ? Also tell me in which format should i write that part. PLEASE

package homework5;

import java.sql.*;

import java.util.Scanner;

public class homework5 {

public static void main(String[] args) throws SQLException, ClassNotFoundException{

ResultSet resultSet = null;

PreparedStatement preparedStmt=null;

Connection connection = null;

Statement statement = null;

Scanner sc =null;

try{

sc=new Scanner(System.in);

System.out.println("Enter the SSN for the student you want to enroll");

Long ssn=sc.nextLong();

System.out.println("Enter the CourseID which the student wants to enroll in");

String courseId =sc.next();

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

System.out.println("Driver loaded");

connection = DriverManager.getConnection

System.out.println("Database connected");

String storedProcSql = "EXEC insertStudent ?,?"; // for stored procedures taking 2 parameters ssn and course id

preparedStmt = connection.prepareStatement(storedProcSql);

preparedStmt.setLong(1, ssn);

preparedStmt.setString(2,courseId);

int rows = preparedStmt.executeUpdate();

System.out.println("No. of rows inserted : " + rows );

// After successful insertion , fetch the row inserted in the following lines

statement = connection.createStatement();

resultSet = statement.executeQuery

("SELECT firstName, lastName, e.courseid, c.title "

+ "FROM students s, course c, enrollment e "

+ "WHERE s.ssn = ? AND e.courseid = ? "

+ "AND e.courseid = c.courseid "); // SSN is of the type LONG here

while (resultSet.next())

System.out.println(resultSet.getString(1) + " "

+ resultSet.getString(2) + " " + resultSet.getString(3) + " "

+ resultSet.getString(4));

}

catch(SQLException sqlExec){

sqlExec.printStackTrace();

}

catch(Exception e){

e.printStackTrace();

}

finally{

resultSet.close();

connection.close();

sc.close();

}

}

}

Explanation / Answer

String storedProcSql = "EXEC insertStudent ?,?";

The above statement can be rewritten as

String storedProcSql = "{call insertStudent(?,?)}";

You can use CallableStatement instead of PreparedStatement for executing stored procedures efficiently

CallableStatement cstmt= connection.prepareCall(storedProcSql);

cstmt.setLong(1, ssn);

cstmt.setString(2,courseId);

int rows = cstmt.executeUpdate();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote