简体   繁体   中英

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM