This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes Create a tree from a resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Create a tree from a resultset " Watch "Create a tree from a resultset " New topic

Create a tree from a resultset

Mike Boota
Ranch Hand

Joined: Jul 18, 2002
Posts: 82

I need to create a tree structure from a resultset and show them into my JSP. Here is my table sql (grup).

What I really need is a recursive method to search for all the children of each row and build a tree heirarchy. e.g. the data may look like

So looking at the top data my final tree structure should look like

and so on. ORDER_BY show the order in which each element should display in the heirarchy. I am really struggling to find a way on how to have the heirarchy built and really need some help.


Here is the SQL i have to het the data

MB<br />Sun Certified Programmer for Java2 Platform
Rob Spoor

Joined: Oct 27, 2005
Posts: 20136

I usually use a Map for this. Something like this (pseudo code):
When done roots contains all root nodes. If you have one single tree it should contain only one element.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Create a tree from a resultset
It's not a secret anymore!