aspose file tools*
The moose likes JDBC and the fly likes Making a String[] from ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Making a String[] from ResultSet" Watch "Making a String[] from ResultSet" New topic
Author

Making a String[] from ResultSet

Kevin Crocker
Greenhorn

Joined: Sep 06, 2002
Posts: 20
I have poured over the API and I can not find a simple way to make a String[] from a ResultSet (rs) that contains only one column of strings.
I can't believe that I would have to increment a counter by iterating through the rs and then use this counter to create my String[] and then again iterating through the rs to populate my String[].
There has to be something more efficient. Is there no row count method?
Please enlighten :roll:
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Use a Vector initially, and then you can use the toArray() method and cast from an Object[] to a String[].
So in your while(rs.next()) { } block, just use the Vector. That way, you iterate through the ResultSet once, and you will have filled your Vector with the data.
Any reason you HAVE to use a String[] anyway? I always just use the toString() method of the Vector if I need a String from the data. But that is me.


GenRocket - A Test Data Generation Platform
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
I don't believe a rowCount method exists, if it does, I'd like to know about it to.
Just a suggestion... Usually if I don't know the size of an array in advance, I use an java.util.ArrayList instead. Then, if you absolutely needed it as a String array, you could use ArrayList.toArray() methods to make an array out of it.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Kevin Crocker
Greenhorn

Joined: Sep 06, 2002
Posts: 20
My only concern with using a Vector cast is that unused Vector capacity may show up as data in the String[].
Question: Does the Object[] returned by Vector.toArray() represent the size or the capacity of the underlying Vector implementation?
Note: I am bound to using String[] by an external API I am forced to use.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

First off, when it comes to Vectors, just say NO!! Use an ArrayList. If you need it synchronized use the "List Collections.synchronizedList(List list)" on the arraylist.
The toArray() method returns an array containing all of the elements ( which is size, not capacity ) in this collection. Try it out, I should work the way you need it to.
Hope this helps,
Jamie
p.s. -> here is a quick snippet of code:
Kevin Crocker
Greenhorn

Joined: Sep 06, 2002
Posts: 20
That is very interesting but really if I knew the size of my ResultSet I would just iterate through and make my array that size and populate.
Again, it means going through my ResultSet twice.
Thanks for all the info.
Question: Anyone know why Java does not have some means to check the size of the ResultSet obtained?
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

That is very interesting but really if I knew the size of my ResultSet I would just iterate through and make my array that size and populate.

In my example, I don't know the size of my resultset?? That is why I used an arraylist! I do, however, know the number of columns in my resultset ( I knew it at compile time, but you can also find out dynamically ) so was able to use a String[] object to represent each column in the row.
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Also, just wondering how you can represent a ResultSet with a String[]? Is there only one column in the resultset or do you mean String[][]?
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Jamie Robertson:
...Is there only one column in the resultset...

Oooh! I think I have to read the fine print! Since there is only one column try this:

Jamie
[ October 04, 2002: Message edited by: Jamie Robertson ]
k Oyedeji
Ranch Hand

Joined: Jul 07, 2002
Posts: 96
Just to add to this thread, i'm coming from other scripting langauges and i cant beleive there is no way to determine the size of a resultset built in. Looping through a resultset(which could be potentially quite large) or having to execute another query to get a count is inefficient.
Is there a wish list or somewhere you can post suggestions for future versions?
Thanks
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
move to the last record (last()?) and then check the row number (getRow()?).
k Oyedeji
Ranch Hand

Joined: Jul 07, 2002
Posts: 96
Matt
Your suggestions seems like a good idea. I'll will still have to move back to the first though if i want to do something with the data right? And will these methods work if the result set is empty?
Thanks
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
k, if memory serves (I'm heading out and will be unable to code a sample myself), you'll probably want to check the status of the recordset... there should be methods such as isBeforeFirst() and a match for the end of recordset...
but you'll definitely need to reposition to the beginning of the recordset after your getRow call to determine the count.
Fudge around w/ all of the booleans and let us know what happens...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Making a String[] from ResultSet
 
Similar Threads
Can't get right results from a query
Ljava/lang/String Error Pls Help...
comma delimited list
Update inside Select statement
problem with resultset in web service method