Nested Loop

I am building a "tree view" and I need to be able to display the information in a specific way. Let's say my data looks like this:

(imagine this as a two column database)
a 1
a 2
a 3
b 4
b 5
b 6

But i need to display it like this:
a: 1
2
3

b: 4
5
6

What is the best way to do this? I don't think that this is too easy in SQL so I am trying to figure out how to do a loop in my servlet to build this. Do I write a loop with a conditional of some kind?