facebook

javax.servlet.ServletException: ResultSet is closed

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #249261 Reply

    krunalshah
    Member

    Hi Anyone

    I am trying to access the resultset inside a while loop for one resultset and it is giving me following error.

    java.sql.SQLException: ResultSet is closed
    at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcResultSet.next(Unknown Source)
    at ch03.data.UtilDatabase.check(UtilDatabase.java:100)
    at ch03.data.Test.main(Test.java:20)

    SqlBean.java

    
    import java.sql.*;
    import javax.sql.*;
    
    class SqlBean
    {
          private static String Driver = "sun.jdbc.odbc.JdbcOdbcDriver";
          private static String ConnectURL = "jdbc:odbc:HelloDsn";
          
          protected Connection getConnection() throws SQLException,ClassNotFoundException
          {
              Class.forName(Driver);
              Connection con = DriverManager.getConnection(ConnectURL,"","");
              return con;
          }
          
          protected Statement getStatement(Connection con) throws SQLException,ClassNotFoundException
          {
              Statement stmt = con.createStatement();
              return stmt;
          }
          
          protected ResultSet getResultset(Statement stmt, String SqlQuery) throws SQLException,ClassNotFoundException
          {
              ResultSet rs = stmt.executeQuery(SqlQuery);
              return rs;
          }
          
    }
    

    UtilDatabase.java

    
    import java.sql.*;
    import javax.sql.*;
    
    import java.util.*;
    
    
    
    public class UtilDatabase
    {
        private SqlBean sqlbean;
        private Connection con;
        private Statement stmt;
        private ResultSet rs;
        private ResultSet rs1;
     
        public UtilDatabase() throws SQLException,ClassNotFoundException
        {
            sqlbean = new SqlBean();
            con = sqlbean.getConnection();
            stmt = sqlbean.getStatement(con);
        }
        
    
        public void check() throws SQLException, ClassNotFoundException
        {
            String SqlQuery = "Select topic_id from MsgDetails";
            rs = sqlbean.getResultset(stmt,SqlQuery);
            while(rs.next())
            {
                System.out.println("Rs.Next");
                SqlQuery = "Select count(topic_id) from MsgDetails GROUP BY topic_id";
                rs1 = sqlbean.getResultset(stmt,SqlQuery);
                if(rs1.next())
                    System.out.println("Rs1.Next");
            }
        }
    }
    

    Test.java

    
    public class Test
    {
        public static void main(String args[])
        {
            try
            {
                UtilDatabase database = new UtilDatabase();
                database.check();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    

    Pls if any one can answer me regarding this problem..

    What cause to close that Resultset or what cause that reference to become null..

    #249265 Reply

    Haris Peco
    Member

    krunalshah ,

    When you call second ResultSet (rs1) with same statement you close first ResultSet (rs)
    If you want 2 ResultSets open in same time you must have 2 Statement objects

    Best

    #249303 Reply

    krunalshah
    Member

    Hi..

    Thanks very much for ur quick reply… I have tried with two statement and it works fine.

    Is there any other way to do the same job with more efficient programming techniques which use less resorces and do it quickly ?

    Thanks
    Krunal

    #249308 Reply

    Haris Peco
    Member

    krunalshah ,

    No, for 2 queries in same time.You have to take care of closing statements and resltsets, only

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: javax.servlet.ServletException: ResultSet is closed

You must be logged in to post in the forum log in