Results 1 to 5 of 5
- 04-04-2009, 04:49 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 11
- Rep Power
- 0
[SOLVED] Traverse database analogous to preorder traversal of graph
Hello,
I have a database table with 2 columns, say child and parent, depicting the parent-child relationship in a k-ary graph. Hence a tuple in child is "is-a-child-of" of the corresponding parent tuple.
Initially, I supply a parent tuple "p" to my algorithm which needs to trace out all the child of "p" and the chid of its child and so on in the hierarchy of "p".
I have tried to do it recursively (sth like preorder traversal of binary tree) but it doesn't work.
Any idea would be highly appreciated.
With the following code, I get the exception: "Resultset is closed"
Example:
Child Parent
B A
C E
D A
E A
F C
G A
H E
I B
J Y
K C
L Y
Initially, p=A
Output:
A
B
I
D
E
C
F
H
G
Java Code:public void recurseChild(String id, java.sql.Connection conn) { try { ResultSet rs; Statement stmt = conn.createStatement(); String cmd="select conceptid1 from Relationship where conceptid2=" + id; if(stmt.execute(cmd)) { rs=stmt.getResultSet(); while(rs.next()) { String ss=rs.getString(1); System.out.println(ss); recurseChild(ss, conn); } } //end if } //end try catch(SQLException s) { System.out.println(s); } }//end recursiveChild()
- 04-04-2009, 04:53 AM #2
Member
- Join Date
- Nov 2008
- Posts
- 11
- Rep Power
- 0
The formatting of child and parent example seems to have been lost so please understand that first column refers to child and second column to parent.
Even in the output, A is the starting point. B, D, E and G are its children. I is a child of B. C and H are children of E. F is a child of C. They are traced in the order given.
- 04-04-2009, 07:46 PM #3
OK, I'll try and explain why this is happening.
The SQL connection will only return one result set at a time, so every time you query it, the previous set is closed. As you are doing a pre-order traversal, only the first record is read before you recurse and make another query. When the inner method returns, the original result set has been closed. To avoid this, you need to copy the entire result set (and close it for clarity) before you make any recursive calls, probably into a FIFO queue.
I think that is the problem, but even if it isn't that solution should solve it.
- 04-06-2009, 03:35 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 11
- Rep Power
- 0
Thanks. Using LIFO queue works (and not FIFO).
- 04-06-2009, 03:54 AM #5
Similar Threads
-
Array traversal issues
By sondratheloser in forum New To JavaReplies: 3Last Post: 08-13-2012, 12:49 AM -
Focus Traversal Demo in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:37 PM -
error with traverse a relations ship
By darkbalder in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-11-2007, 05:25 PM -
Preorder Traversal problem...
By tuckker in forum New To JavaReplies: 3Last Post: 12-04-2007, 06:06 AM -
How do you recursively traverse through file folders ?
By nimraj in forum New To JavaReplies: 2Last Post: 11-27-2007, 01:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks