aspose file tools*
The moose likes JDBC and the fly likes db method return type: object array or Vector? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "db method return type: object array or Vector?" Watch "db method return type: object array or Vector?" New topic
Author

db method return type: object array or Vector?

Matt Dole
Greenhorn

Joined: Mar 06, 2001
Posts: 17
i have a method that needs to return a collection of objects, say Customers. each row of a table represents the fields of the Customer object. performancewise, is it better to have the method return an array of Customers (ie Customer[]) or a java.util.List/java.util.Vector containing Customer objects?
I personally like the method returning Customer[] because it makes a cleaner method signature.
since i don't know the number of objects that will be returned beforehand (size of array to allocate), i could do 2 queries: select count(*) and the data gather query. Alternatively, i could use 1 query, make a List, then call List's toArray(Object[] a) method. Is there major overhead with that method?
How do you guys deal with methods that return objects created from db queries?
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Originally posted by Matt Dole:
i have a method that needs to return a collection of objects, say Customers. each row of a table represents the fields of the Customer object. performancewise, is it better to have the method return an array of Customers (ie Customer[]) or a java.util.List/java.util.Vector containing Customer objects?
I personally like the method returning Customer[] because it makes a cleaner method signature.
since i don't know the number of objects that will be returned beforehand (size of array to allocate), i could do 2 queries: select count(*) and the data gather query. Alternatively, i could use 1 query, make a List, then call List's toArray(Object[] a) method. Is there major overhead with that method?
How do you guys deal with methods that return objects created from db queries?

In comparison to the performance of the DB connection, passing back an array of objects or an object is going to make negligible difference.
I'd try to avoid doing two hits on the DB. Your solution sounds totally reasonable. I am actually trying to figure out a new design for my db access classes and I was looking at Core J2EE Patterns' Data Access Object and at Martin Fowler's site on Enterprise Application Architecture ( http://martinfowler.com/ ) but nothing really goes into the nitty gritty stuff.
My current db access classes return CachedRowSets which can be disconnected without dying, and the class conveniently has a .size() method.

Adam


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: db method return type: object array or Vector?