Please send your Questions & Answers or Feedback to "mohan@javabook.org"

Which is the preferred collection class to use for storing database result sets?

When retrieving database results, the best collection implementation to use is the LinkedList. The benefits include:

* Retains the original retrieval order

* Has quick insertion at the head/tail

* Doesn't have an internal size limitation like a Vector where when the size is exceeded a new internal structure is created (or you have to find out size beforehand to size properly)

* Permits user-controlled synchronization unlike the pre-Collections Vector which is always synchronized

Basically:

ResultSet result = stmt.executeQuery("...");

List list = new LinkedList();

while(result.next()) {

list.add(result.getString("col"));

}

If there are multiple columns in the result set, you'll have to combine them into their own data structure for each row. Arrays work well for that as you know the size, though a custom class might be best so you can convert the contents to the proper type when extracting from databse, instead of later.

Related Posts Plugin for WordPress, Blogger...
Flag Counter