ResultSet遍历

/**
* 逐行获取数据库数据
*
* @param rs
* @return
* @throws SQLException
*/
private 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++) {
if (md.getColumnName(i).contains(“date”)) {
if (rs.getObject(i) != null) {
rowData.put(md.getColumnName(i), rs.getObject(i).toString().substring(0, 19));// 获取键名及值
} else {
rowData.put(md.getColumnName(i), “”);
}
} else {
if (rs.getObject(i) != null) {
rowData.put(md.getColumnName(i), rs.getObject(i));// 获取键名及值
} else {
rowData.put(md.getColumnName(i), “”);
}
}
}
list.add(rowData);
}
return list;
}

你可能感兴趣的:(ResultSet遍历)