Java ResultSet 转 list
ResultSet是java中执行sql语句后生成的结果集
/**
* resultset 转list
*
* @param rs
* @return
* @throws SQLException
*/
private static List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();//获取键名
int columnCount = md.getColumnCount();//获取行的数量
while (rs.next()) {
Map rowData = new HashMap();//声明Map
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));//获取键名及值
}
list.add(rowData);
}
return list;
}
/**
* resultset 转 HashMapList
*
* @param rs
* @return
* @throws SQLException
*/
public static ArrayList<HashMap<String, Object>> ResultToHashMapList(ResultSet rs) throws SQLException {
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
ResultSetMetaData md = rs.getMetaData();//获取键名
int columnCount = md.getColumnCount();//获取列的数量
while (rs.next()) {
HashMap<String, Object> rowData = new HashMap<>();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));//获取键名及值
}
list.add(rowData);
}
return list;
}