Makan
Makan

Reputation: 2736

Get all values in the row `java.sql.ResultSet` in one go

Using the java.sql.ResultSet Class, I want to get all the cells in a row as an Object List, or Object array, and postpone the processing/fetching of each cell, for performance concerns. The following method:

Object obj = resultSet.getObject(i);

should be called columnCount number of times.

What is the fastest way? / Why is a seemingly obvious functionality missing?

Upvotes: 0

Views: 3242

Answers (1)

Marc Le Bihan
Marc Le Bihan

Reputation: 3304

I think this might do what you want. It should return all the columns values of the ResultSet current row.

List<Object> getValues(ResultSet resultSet) {
   ResultSetMetaData metadata = resultSet.getMetadata();
   int numberOfCols = metadata.getColumnCount();

   List<Object> values = new ArrayList<>();

   for(i=1; i <= numberOfCols; i ++) {
      values.add(resultSet.getObject(i));
   }

   return values;
}

Upvotes: 3

Related Questions