Salini L
Salini L

Reputation: 879

Error occure while using two resultset in same table

I am a beginner. In my project I am using Java and Mysql. While doing a code I got an error. That code is mentioning below

<%
       ResultSet rs=s.fetchtask(userid);
       while(rs.next())
       {
%> 
         <table border="1">
             <tr><td>
              <% int qnid=(rs.getInt("question_id")); 
                 ResultSet rs1=s.fetchqn(qnid);
                 String qn=rs1.getString("question");
              %> 
                  <% out.println(qn);%>
               </td></tr>
         </table>
      <%
          }
       %> 

But when I am not using the second fetch its working

<%
       ResultSet rs=s.fetchtask(userid);
       while(rs.next())
       {
%> 
         <table border="1">
             <tr><td>
              <% int qnid=(rs.getInt("question_id"));     
              %> 
                  <% out.println(qnid);%>
               </td></tr>
         </table>
      <%
          }
       %> 

When I am using this code there is no problem. Please somebody help me. The error showing is

org.apache.jasper.JasperException: An exception occurred processing JSP page /setter.jsp at line 142

 139:                        // ResultSet rs1 = st.executeQuery("SELECT * FROM temp_qb  WHERE question_id="+qnid+"");
 140:                         ResultSet rs1=s.fetchqn(qnid);
 141:                           //  ResultSet rs1=s.fetchqn(qnid);
 142:                         String qn=rs1.getString("question");
 143:                          %> 
 144:                          <% //out.println(qn);
 145:                          %>

Upvotes: 0

Views: 218

Answers (2)

Salini L
Salini L

Reputation: 879

I have solved it. The problem was only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists. SO by using another statement it can be solved.

Upvotes: 1

Praveen Lobo
Praveen Lobo

Reputation: 7187

  • You are not checking the result set to make sure you have the data. That could lead to exceptions.

With so little to go on, I suggest you try this and see if you still get the same error.

<%
       ResultSet rs=s.fetchtask(userid);
       while(rs.next())
       {
%> 
         <table border="1">
             <tr><td>
              <% int qnid=(rs.getInt("question_id")); 
                 ResultSet rs1=s.fetchqn(qnid);
                 while(rs1.next())
                 { 
                    String qn=rs1.getString("question");
                    out.println(qn);
                 } 
              %>
               </td></tr>
         </table>
<%
       }
%> 
  • Also make sure you are using the correct column name to fetch the data from the result set.
  • Take care to close the result set and db connections properly once you are done using them.

Finally,

  • Please avoid using scriplets if you are building something new.

Upvotes: 0

Related Questions