1

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?

Makan
  • 2,508
  • 4
  • 24
  • 39
  • 1
    How about just using a loop? – JB Nizet Jul 25 '19 at 11:01
  • And how exactly do you propose to avoid "fetching"? This smells like a massively premature optimization. – chrylis -cautiouslyoptimistic- Jul 25 '19 at 11:10
  • Wouldn't a Obj[] getAllObjects() reduce number of function calls? 'column' fold? Why is it untimely for thinking about speed? – Makan Jul 25 '19 at 12:43
  • Maybe because no one wants an array of Object where they have to type cast every element – Joakim Danielson Jul 25 '19 at 14:53
  • In my usecase, which I think is not too specific, I load the data and some metadata, ship it to some frontend module, possibly in the future, on a separate machine. The frontend will take care of the casting and processing further. While / Because my backend would be my bottleneck. Therefore it would have been nice to have the function. I hope I am not raising eyebrows with my design :) – Makan Jul 26 '19 at 08:14

1 Answers1

2

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=0; i < numberOfCols; i ++) {
      values.add(resultSet.getObject(i));
   }

   return values;
}
Marc Le Bihan
  • 2,308
  • 2
  • 23
  • 41