Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making a String[] from ResultSet

 
Kevin Crocker
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kevin Crocker
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
move to the last record (last()?) and then check the row number (getRow()?).
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic