File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes iterating through ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "iterating through ResultSet" Watch "iterating through ResultSet" New topic

iterating through ResultSet

joew weakers
Ranch Hand

Joined: Aug 21, 2003
Posts: 48
Hi there. I am running a simple SQL query that returns a result set containing two columns. A sample set of results appears as follows:

D43 73
D43 78
D43 87
D44 65
D65 102
D65 107
D65 111
D65 115
D85 77
... ...

What I would like to do is for every distinct value encountered in the first column, create an object and set an array/vector of size equal to the number of values in the second column associated with that first column value. For example, an object called 'D43_object' that has a vector of size 3 containing the 3 ids associated with cfcc 'D43'. My attempt at coding this is as follows:

I know why my code does not work but I do not know how to go about altering it so that objects are instantiated correctly and how I can create new objects only for certain rows in the result set and not for every row in the result set. Can anybody show me how I might fix this. Thnaks a lot, Joe
Denise Hum

Joined: Oct 09, 2001
Posts: 20
Have you considered the idea of using a Hashtable with cfcc as the key and the vector of Ids as a value? This way you don't have to worry about the order of the the cfcc's being returned and I think you'd have to do less checking.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Make sure you have an order by clause in your SQL statment so the cfcc's are returned in order.

The idea looks fine. Although "that has a vector of size 3 " is more of an implicit requirement. You will wind up with a Vector with three elements in it. But you don't need to know there are three before you start out.

The only thing I don't see is the collection you are storing all the LayerFeature objects in.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
joew weakers
Ranch Hand

Joined: Aug 21, 2003
Posts: 48
Hi Jeanne, Denise. Thanks a lot for your replies. Denise, I have not tried using Hsahtables just yet but I may use them in the future. Jeanne, I did as you suggested but I am still encountering the following error:

variable landmark_ids might not have been initialized

The code is as you specified:

Its kind of like the error I had initially. Have you any ideas. Cheers, joe
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Change this:

To this:

wood burning stoves
subject: iterating through ResultSet
It's not a secret anymore!