What I know-we cannot call ResultSet
after this has been closed.and this is a good practice to close every ResultSet
,Statement
andConnection
after using this.
But in my code I never closed connection then why there is-
java.sql.SQLException: Operation not allowed after ResultSet closed
my code is as follows:
Edited-
try{
Connection con=CommonUtil.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from logirecord");
int flag=0;
while(rs.next()){
if(rs.getString(2).trim().equals(username)&&rs.getString(3).trim().equals(password)){
flag=1;
ResultSet rs1=st.executeQuery("select * from personrecord where LoginId='"+ rs.getString(1).trim()+"'");
if(rs1.next()){
String name=rs1.getString(1);
String address=rs1.getString(2);
String hobby=rs1.getString(4);
}
//Exception here.
ResultSet rs2=st.executeQuery("select * from interest where LoginId='"+rs.getString(1).trim()+"'");
//at com.org.test.LoginServlet.doPost(LoginServlet.java:49)
if(rs2.next()){
String interest=rs2.getString(2);
String interest2=rs2.getString(3);
String interest3=rs2.getString(4);
}
request.getRequestDispatcher("display.jsp").forward(request, response);
return;
}
}
}catch(Exception e){
e.printStackTrace();
}
Well you are getting
java.sql.SQLException: Operation not allowed after ResultSet closed
Because you are trying to traverse a result set while traversing another result set of same connection.
If you're nesting the processing of two result sets from the same database, you're doing something wrong. The combination of those sets should be done on the database side.