• Post Reply Bookmark Topic Watch Topic
  • New Topic

From SqlRowSet to DefaultMutableTreeModel  RSS feed

 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Right now I'm working with a SQL query that's returning a series of parent-child joins, something similar to the following:

Maybe this is more of a beginner question, but how can I use a SqlRowSet to parse these rowsets and build a tree model? I'm totally lost on where to even begin.

I'm thinking of looping through the rowset like this, maybe:


...but of course it's not going to be that simple. Has anyone else worked with something similar to this?

Thanks in advance.

[ May 22, 2006: Message edited by: Trevor Price ]
[ May 22, 2006: Message edited by: Trevor Price ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Trevor Price:

...but of course it's not going to be that simple.


It's not? Why?
 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


It's not? Why?


Because as I loop through the SqlRowSet, I would be duplicating each grandparent node several times, and also the same with parent nodes.

I could do a check to see if a given grandparent/parent node already exists (I
suppose a HashMap <String, DefaultMutableTreeNode> would work), then if it doesn't exist I would create it.

Say:


However, I'm looking at a tree containing several layers, and maybe over 7000 nodes in the end. This seems like a very inefficient way of populating it.
 
Jaap Vermeer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Trevor Price:




This is not a very efficient way to put a tree in a database. A better way (in some respects, not all) is this:

No data redundancy. It could be a bit harder to build a tree from this, though.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Trevor Price:
This seems like a very inefficient way of populating it.


No, this really isn't bad. HashMap access time is amortized constant. What I tend to do in this kind of situation is hide about half of this behind a method called getNodeForName(), and the other half behind addChildNode(); both of these methods would look for preexisting nodes before creating one, or preexisting children before adding one. Then just iterate through the data, ask for each node as you need it, and add it as you come to it; the auxilliary methods will hide the complexity. As I said, using HashMaps will make it very efficient.
 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaap Vermeer:

<hr></blockquote>

This is not a very efficient way to put a tree in a database. A better way (in some respects, not all) is this:

No data redundancy. It could be a bit harder to build a tree from this, though.[/QB]


Yeah, that's actually how it's stored. The SQL query that I'm using is joining tables through parent-child relationships so that it looks like the first resultset.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!