posted 16 years ago
You loop over all the rows, put keep overwriting the same positions inside the data array, so yeah, only the last row will be available. You really need to redesign. Here are some options:
1) Use a 2D Object array:
Then when iterating over the result:
2) Make the data array long enough to hold all the rows and columns, so instead of Object[] data = new Object[colcount] it would be Object[] data = new Object[rowCount * colCount]; Then you have to keep track of what your offsets are in terms of both row and column:
3) Make a DataTransferObject which has a meaningful representation of the Row and fill in the correct values from the result set. Then pass back an array of the data transfer object. This would make handling the data a lot more maintenance friendly and understandable. As an example, if your database row represents the information about a Client, and the columns where:
[First Name, Last Name, Company, Position]
then your DTO might look like:
And when you gather data from the database it could look like:
Of course it would be a lot easier to pass around a generic list of type Client, or a Client[] instead of Object[] at this point...
Of the three options, 2) is the worst, and 3) is the best, in my opinion.