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

  • SEARCH
  • Home
  • 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 8509497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:27:01+00:00 2026-06-11T03:27:01+00:00

As I’ve started in the title, while I’m querying for user data in my

  • 0

As I’ve started in the title, while I’m querying for user data in my java application, I get following message: “Operation not allowed after ResultSet closed”.

I know that this is happens if you try to have more ResultSets opened at the same time.

Here is my current code:

App calls getProject(“…”), other 2 methods are there just for help. I’m using 2 classes because there is much more code, this is just one example of exception I get.

Please note that I’ve translated variable names, etc. for better understanding, I hope I didn’t miss anything.

/* Class which reads project data */

public Project getProject(String name) {
    ResultSet result = null;
    try {
        // executing query for project data
        // SELECT * FROM Project WHERE name=name
        result = statement.executeQuery(generateSelect(tProject.tableName,
                "*", tProject.name, name));
        // if cursor can't move to first place,
        // that means that project was not found
        if (!result.first())
            return null;
        return user.usersInProject(new Project(result.getInt(1), result
                .getString(2)));
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    } catch (BadAttributeValueExpException e) {
        e.printStackTrace();
        return null;
    } finally {
        // closing the ResultSet
        try {
            if (result != null)
                result.close();
        } catch (SQLException e) {
        }
    }
}

/* End of class */

/* Class which reads user data */

public Project usersInProject(Project p) {
    ResultSet result = null;
    try {
        // executing query for users in project
        // SELECT ID_User FROM Project_User WHERE ID_Project=p.getID()
        result = statement.executeQuery(generateSelect(
                tProject_User.tableName, tProject_User.id_user,
                tProject_User.id_project, String.valueOf(p.getID())));

        ArrayList<User> alUsers = new ArrayList<User>();
        // looping through all results and adding them to array
        while (result.next()) { // here java gets ResultSet closed exception
            int id = result.getInt(1);
            if (id > 0)
                alUsers.add(getUser(id));
        }
        // if no user data was read, project from parameter is returned
        // without any new user data
        if (alUsers.size() == 0)
            return p;
        // array of users is added to the object,
        // then whole object is returned
        p.addUsers(alUsers.toArray(new User[alUsers.size()]));
        return p;
    } catch (SQLException e) {
        e.printStackTrace();
        return p;
    } finally {
        // closing the ResultSet
        try {
            if (result != null)
                result.close();
        } catch (SQLException e) {
        }
    }
}

public User getUser(int id) {
    ResultSet result = null;
    try {
        // executing query for user:
        // SELECT * FROM User WHERE ID=id
        result = statement.executeQuery(generateSelect(tUser.tableName,
                "*", tUser.id, String.valueOf(id)));
        if (!result.first())
            return null;
        // new user is constructed (ID, username, email, password)
        User usr = new user(result.getInt(1), result.getString(2),
                result.getString(3), result.getString(4));
        return usr;
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    } catch (BadAttributeValueExpException e) {
        e.printStackTrace();
        return null;
    } finally {
        // closing the ResultSet
        try {
            if (result != null)
                result.close();
        } catch (SQLException e) {
        }
    }
}

/* End of class */

Statements from both classes are added in constructor, calling connection.getStatement() when constructing each of the classes.

tProject and tProject_User are my enums, I’m using it for easier name handling. generateSelect is my method and should work as expected. I’m using this because I’ve found out about prepared statements after I have written most of my code, so I left it as it is.

I am using latest java MySQL connector (5.1.21).

I don’t know what else to try. Any advice will be appreciated.

  • 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-11T03:27:03+00:00Added an answer on June 11, 2026 at 3:27 am

    Quoting from @aroth’s answer:

    There are many situations in which a ResultSet will be automatically closed for you. To quote the official documentation:
    http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html

    A ResultSet object is automatically closed when the Statement object that generated 
    it is closed, re-executed, or used to retrieve the next result from a sequence of 
    multiple results.
    

    Here in your code , You are creating new ResultSet in the method getUser using the same Statement object which created result set in the usersInProject method which results in closing your resultset object in the method usersInProject.

    Solution:
    Create another statement object and use it in getUser to create resultset.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.