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 7589167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:05:43+00:00 2026-05-30T20:05:43+00:00

I have a question regarding ResultSet objects in Java and recursion. I was working

  • 0

I have a question regarding ResultSet objects in Java and recursion.

I was working on some code for university and whenever I found a descendant I recursed on with that new node but when I came out of the recursion and tried to rs.next() the pointer had gone from pointing to row 1 back to row 0 and when it hit row 0 the rs.next() failed and it returned! I knew there was one thing in there that it hadn’t read yet! What is it that causes this?

The only way I got round that problem was to go through the resultset and get every element and add it into an array list, then loop through the arraylist doing the recursion on each element in the array! Surely this must be a better way around this?

This is the new code I’m using

private Vector<String> getDescendents(String dogname, Vector<String> anc) {
    if (anc == null) anc = new Vector<String>();
    ArrayList<String> tempList = new ArrayList<String>(2);
    try {
        System.out.println("Inside ");
        childStmt.setString(1,dogname);
        childStmt.setString(2,dogname);
        ResultSet rs = childStmt.executeQuery();
        System.out.println("Before while "+rs.getRow());
        while (rs.next()){
            String col1 = rs.getString(1);
            tempList.add(col1);
            anc.add(col1);
        } 
        for (String s:tempList){
            getDescendents(s,anc);
        }

    }
    catch(Exception e) {
        doError(e, "Failed to execute ancestor query in getBreeding");
    }
    return anc;
}

However before this, I had the getDescendents call inside the while loop and thus no for loop and no arraylist either, but whenever it actually recursed it would loose track of the resultset when it returned out of the recursion.

Further details :
When I used the debugger (nearly said gdb there lol far too much C) the ID of the result set was the same but the row pointer had returned to 0 and the rs.next call failed!

Once again any explanation is appreciated!

p.s it previously looked like

private Vector<String> getDescendents(String dogname, Vector<String> anc) {
    if (anc == null) anc = new Vector<String>();
    ArrayList<String> tempList = new ArrayList<String>(2);
    try {
        System.out.println("Inside ");
        childStmt.setString(1,dogname);
        childStmt.setString(2,dogname);
        ResultSet rs = childStmt.executeQuery();
        System.out.println("Before while "+rs.getRow());
        while (rs.next()){
            String col1 = rs.getString(1);
            anc.add(col1);
            getDescendendts(col1,anc);
        } 

    }
    catch(Exception e) {
        doError(e, "Failed to execute ancestor query in getBreeding");
    }
    return anc;
}
  • 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-05-30T20:05:45+00:00Added an answer on May 30, 2026 at 8:05 pm

    It looks like you’re re-using childStmt; don’t do this. From the Statement javadoc:

    By default, 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.

    You’ll have to either save all the rows first, then do the recursive query, or create a new Statement for each ResultSet you want to fetch.

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

Sidebar

Related Questions

I have a question regarding the integration of business objects developed using Linq To
i have a question regarding this Haskell code: module Queue (Queue, emptyQueue, queueEmpty, enqueue,
I have a simple question regarding java generics. How do I construct a generic
this is a noobie question regarding tree maps. I have read through the Java
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I have a question regarding handling errors in a J2EE application. Our current application
I have one question regarding domain account. I have one domain controller where all

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.