Results 1 to 5 of 5
  1. #1
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

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

  2. #2
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    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.

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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.

  4. #4
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Thanks. Using LIFO queue works (and not FIFO).

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Ah, I always get them the wrong way round until I notice I'm traversing the wrong way.

Similar Threads

  1. Array traversal issues
    By sondratheloser in forum New To Java
    Replies: 3
    Last Post: 08-13-2012, 12:49 AM
  2. Focus Traversal Demo in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:37 PM
  3. error with traverse a relations ship
    By darkbalder in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-11-2007, 05:25 PM
  4. Preorder Traversal problem...
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-04-2007, 06:06 AM
  5. Replies: 2
    Last Post: 11-27-2007, 01:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •