Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:16:33+00:00 2026-06-16T01:16:33+00:00

I have class called Database. public class Database { public Connection connect = null;

  • 0

I have class called Database.

    public class Database {
    public Connection connect = null;
    public Statement st = null;
    public PreparedStatement ps = null;
    public ResultSet rs = null;

    public boolean connectDB() throws Exception {
      try {

      Class.forName("com.mysql.jdbc.Driver");

      connect = DriverManager
        .getConnection("jdbc:mysql://localhost/ots?"
              + "user=root&password=mongolia");
    } catch (Exception e) {
      System.out.println(e);
    }
    return true;
   }

    public void disconnectDB() {
    try {
      if (rs != null) {
        rs.close();
      }

      if (st != null) {
        st.close();
      }

      if (connect != null) {
        connect.close();
      }
    } catch (Exception e) {

    }
    }

     } 

and class called user which is extending Database class

public class User extends Database {
    public ResultSet fetchTable(){
        try{
            connectDB();            
            st = connect.createStatement();
            rs = st.executeQuery("SELECT * FROM user");         
        }catch(Exception e){
            System.out.println(e);
        }finally{
            disconnectDB();
        }
        return rs;
    }
  }
//Inside JSP page
  User user = new User();
  ResultSet data = user.fetchTable();

  //getting exception in the data.next() function
  //java.sql.SQLException: Operation not allowed after ResultSet closed

  while(data.next()){
        out.println("<p>"+data.getInt(0)+"</p>");
   }

//getting exception in the data.next() function
//java.sql.SQLException: Operation not allowed after ResultSet closed
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:16:34+00:00Added an answer on June 16, 2026 at 1:16 am

    The exception is entirely expected. You’re connecting the DB, obtaining a result set, closing the DB and the result set and then trying to access the closed result set.

    This is not how things are supposed to work in JDBC.

    You need to map the result set to a List<User> directly after retrieving it and then close the result set and return the List<User> instead.

    For some concrete examples, head to the answer on this question: JDBC driver throws "ResultSet Closed" exception on empty ResultSet


    Unrelated to the concrete problem, you’ve other severe problems in the code. Among others, you have declared the Connection, Statement and ResultSet as instance variables instead of as method local variables. This will fail hard when the same instance is been shared between multiple threads (which may occur when two or more users simultaneously access your web application). I’d also fix on that.


    Update: the other answers posted so far recommend to remove the disconnectDB() call or to call it only after iterating through the result set in the other method. This is wrong. You should not pass the ResultSet out of the method. Your code would be still threadunsafe and you would still risk resource leaking in case of exceptions. You should create, use and close it in the very same method block. Here’s the proper approach, copypasted from the aforementioned question:

    public List<User> list() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> users = new ArrayList<User>();
    
        try {
            connection = database.getConnection();
            statement = connection.prepareStatement("SELECT id, username, email, age FROM user");
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                users.add(new User(
                    resultSet.getLong("id"),
                    resultSet.getString("username"),
                    resultSet.getString("email"),
                    resultSet.getInteger("age")));
            }
        } finally {
            close(resultSet, statement, connection);
        }
    
        return users;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Account public class Account { public double balance =
So currently my class DATABASE uses a singleton for a connection and is called
I have a generic class called db which talks directly to the database. And
I have a class called: ComplexNumber and I have a string that I need
I have a class called Trial which has_many results. Now What I want to
I have a class called Metadata, which is declared within the namespace A::B::C ,
I have a class called Flamethrower which naturally has its own ammunition that is
I have a class called Property which has nothing but get -methods. All the
I have one class called Global and two other activities. In each activity I
I have a class called Property that I only need to display a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.