You can either write a data transfer object that encapsulates all those columns, OR you can save them in a List of Maps, where each Map represents one row and the keys are the column names. Here's a method that will do it for you:
/**
* Helper method that converts a ResultSet into a list of maps, one per row
* @param query ResultSet
* @return list of maps, one per row, with column name as the key
* @throws SQLException if the connection fails
*/
public static final List toList(ResultSet rs) throws SQLException
{
List wantedColumnNames = ResultSetMapper.getColumnNames(rs);
return ResultSetMapper.toList(rs, wantedColumnNames);
}
/**
* Helper method that maps a ResultSet into a list of maps, one per row
* @param query ResultSet
* @param list of columns names to include in the result map
* @return list of maps, one per column row, with column names as keys
* @throws SQLException if the connection fails
*/
public static final List toList(ResultSet rs, List wantedColumnNames) throws SQLException
{
List rows = new ArrayList();
int numWantedColumns = wantedColumnNames.size();
while (rs.next())
{
Map row = new TreeMap();
for (int i = 0; i < numWantedColumns; ++i)
{
String columnName = (String)wantedColumnNames.get(i);
Object value = rs.getObject(columnName);
row.put(columnName, value);
}
rows.add(row);
}
return rows;
}
/**
* Return all column names as a list of strings
* @param database query result set
* @return list of column name strings
* @throws SQLException if the query fails
*/
public static final List getColumnNames(ResultSet rs) throws SQLException
{
List columnNames = new ArrayList();
ResultSetMetaData meta = rs.getMetaData();
int numColumns = meta.getColumnCount();
for (int i = 1; i <= numColumns; ++i)
{
columnNames.add(meta.getColumnName(i));
}
return columnNames;
}
Hope this will help
Cheers
