將ResultSet轉換成List
阿新 • • 發佈:2019-02-04
public List<HashMap<String,String>> thansResultSet2List() throws SQLException{
List<HashMap<String,String>> result=new ArrayList<HashMap<String,String>>();
int columnCount = md.getColumnCount(); //返回此 ResultSet 物件中的列數
try {
while(rs.next()){
map=new HashMap<String,String>(columnCount);
for(int i = 1; i <= columnCount; i++){
map.put(md.getColumnName(i), String.valueOf(rs.getObject(i)));
}
result.add(map);
}
System.out.println("result:"+result.toString());
return result;
} catch (SQLException e) {
addException(e, DatabaseException.RS_ROW_EXCEPTION);
return null;
}
}
List<HashMap<String,String>> result=new ArrayList<HashMap<String,String>>();
HashMap<String,String> map=new HashMap<String,String>();
if(rs==null)return null;
ResultSetMetaData md = rs.getMetaData(); //得到結果集(rs)的結構資訊,比如欄位數、欄位名等int columnCount = md.getColumnCount(); //返回此 ResultSet 物件中的列數
try {
while(rs.next()){
map=new HashMap<String,String>(columnCount);
for(int i = 1; i <= columnCount; i++){
map.put(md.getColumnName(i), String.valueOf(rs.getObject(i)));
}
result.add(map);
}
System.out.println("result:"+result.toString());
return result;
} catch (SQLException e) {
addException(e, DatabaseException.RS_ROW_EXCEPTION);
return null;
}
}