1. The easy way if you are using an oracle database, this gets them all in
one query

select name
from table
start with id = ?
connect by parent = prior id

2. use a stack
Brief example...

class Person {
int id;
String name;

   Person(int id, String name) {
      this.id = id;
      this.name = name;
   }
}

// Create node to start with
Person root = new Person(4, "Jackie");
Stack stack = new Stack();

// Place starting node on stack
stack.push(root);
Connection con = ... get connection here
PreparedStatement ps = con.prepareStatement("select id, name from table
where parent = ?");
do {
   Person current = (Person) stack.pop();
   // Print current item on stack
   out.println("<b>" + current.name + "</b><br>");
   // Set parent to look for
   ps.setInt(1, person.id);

   // Get children
   ResultSet rs = ps.executeQuery();
   while (rs.next()) {
     // Place children on stack
     stack.push(new Person(rs.getInt(1), rs.getString(2)));
   }
   rs.close();
} while(!stack.isEmpty()); // Continue as long as there are more children

ps.close();
con.close();

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to