ResultSet reset when using recursion

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

I worked on some code for the university, and whenever I discovered a descendant, I returned to this new node, but when I left recursion and tried to use rs.next (), the pointer moved away from pointing to line 1 back to line 0 and when it hit line 0, rs.next () failed and it came back! I knew that there was one thing that he had not read yet! What does it make?

The only way I ran into this problem was to go through the result set and get each element and add it to the list of arrays, and then break through the arraylist, recursing for each element of the array! Could this be better?

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 that I had a call to getDescendents inside the while and therefore not for the loop and there was no arraylist, but whenever it actually returned, it would lose track of the result set when it returned from recursion.

Additional info: When I used the debugger (almost said that gdb had too much C there), the result set identifier was the same, but the line pointer returned to 0, and the rs.next call failed!

Again all explanations will be appreciated!

ps he used to look 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; } 
+4
source share
1 answer

It looks like you are reusing childStmt ; do not do this. From Statement javadoc :

By default, only one ResultSet can be opened for a Statement. in the same time. Therefore, if reading one ResultSet object alternates with reading another, each must be generated by different Statement objects. All execution methods in the Application interface implicitly closes the current ResultSet if one exists.

You need to either save all rows first, or execute a recursive query, or create a new Statement for each ResultSet that you want to receive.

+12
source

Source: https://habr.com/ru/post/1399455/


All Articles