• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL Results To Collection

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking for the best way of storing a result of sql query results in a collection.

The sql results look similar to the following:

100 Tg Sample1
101 Th Sample5
102 Tk Sample8
103 Tf Sample7
104 Ts Sample2

Would a multidimensional array be best for storing the results so that they can be extracted element by element when required.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really. For one thing you don't know the number of rows until you have read them all, and for another thing it is not object-oriented.

Create a class that represents a row of your table. Each time you read a row of the table, create an object of that class and fill it with the column data. (This is called a DAO, Data Access Object). Then add that object to a collection; ArrayList would do nicely.
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option you might want to consider is having a factory class or method somewhere that provides the java.util.Collection object, instead of making it obvious you are using ArrayList or whatever. Just use an Iterator to traverse the collection instead of using get(i) or size(), so you don't have an implicit assumptions about the nature of the Collection.

It seems most applications running against a daatabase sooner or later evolve into having more data to return than you want to actually have in memory at one time. If you hide your actual Collection (or List, or Map) implementation, you gain the opportunity to change the Iterator implemention too. That lets you play the same kinds of games as EJB CMP and CMR, where you can hide the result set dependency inside the Iterator.
[ February 03, 2006: Message edited by: Reid M. Pinchback ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic