I am trying to return a resultset of a given student id and show the gpa. However it is saying I am not able to do the operation when the result set is closed. This is my code:
/**
* Returns a ResultSet with a row containing the computed GPA (named as gpa) for a given student id.
* You must use a PreparedStatement.
*
* @return
* ResultSet containing computed GPA
*/
public ResultSet computeGPA(String studentId) throws SQLException
{
ResultSet rst;
// TODO: Use a PreparedStatement
try(Connection con = connect()){
String sql = "SELECT gpa FROM student WHERE sid=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, studentId);
rst = pstmt.executeQuery();
while(rst.next()){
System.out.println(rst.getString("gpa"));
}
}
return rst;
}
This is the result im getting
Connecting to database. 2.96 Exception in thread "main" java.sql.SQLException: Operation not allowed after ResultSet closed at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920) at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:804) at com.mysql.jdbc.ResultSetImpl.getMetaData(ResultSetImpl.java:3107) at EnrollJDBC.resultSetToString(EnrollJDBC.java:500) at EnrollJDBC.main(EnrollJDBC.java:59)