[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
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()