Java implements the JDBC query result set result to be converted to the corresponding list set
- 2020-04-01 04:31:45
- OfStack
The code is very simple, so there's no more crap here, just serve it
public static <T> List<T> convertToList(ResultSet rs,Class<T> t) throws SQLException {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
Map<String, Object> rowData = new HashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
JSONArray jr = JSONArray.fromObject(list);
List<T> resultList = JSONArray.toList(jr, t);
return resultList;
}
Isn't that easy. For the novice reference, the old birds please skip