1. 程式人生 > >將ResultSet轉換成List

將ResultSet轉換成List

public List<HashMap<String,String>> thansResultSet2List() throws SQLException{
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;
}

}