请问在java中如何将ResultSet转换为map数组?
感谢!
是不是这样子:
[code="java"]
public static void main(String[] args)throws Exception {
ResultSet rs=null;//自己赋值
ArrayList> list=new ArrayList>();
while(rs.next()){
Map row=new HashMap();
row.put("id", rs.getString(0));
row.put("Amount", rs.getBigDecimal(1));
//...其他数据
list.add(row);
}
Map[] arr=list.toArray(new HashMap[list.size()]);
}
[/code]
[code="java"]Map map=new HashMap();
while(rs.next()){
map.put("username",rs.getString(1));
map.put("sex",rs.getString(2));
}[/code]
很好办
1,首先查出总数count
2,
[code="java"]Map[] mapObj=new HashMap[count];
int i=0;
while(rs.next()){
Map map=new HashMap();
String userid=rs.getString(1);
map.put("userid",userid);
map.put("username",rs.getString(2));
map.put("sex",rs.getString(3));
mapObj[i]=map;
i++;
}[/code]
lz没说清楚啊,你是想map数组,而每个map里面到底是放的map还是对象。
而且用map数组不好啊,你还得查总数(因为数组声明是必须要知道长度的)
你对着自己变换一下就行了。
jdbc的ResultSet转换map,自己转换很烦琐,建议使用一些开源的类库,比如Spring的JDBCTemplate,apache的Dbutils都不错。
[code="java"]1.public static void main(String[] args)throws Exception {
2. ResultSet rs=null;//自己赋值
3. ArrayList> list=new ArrayList>();
4. while(rs.next()){
5. Map row=new HashMap();
6. row.put("id", rs.getString(0));
7. row.put("Amount", rs.getBigDecimal(1));
8. //...其他数据
9. list.add(row);
10. }
11. Map[] arr=list.toArray(new HashMap[list.size()]);
12.} [/code]
Map[] maps = new Map[10];
//长度可以根据rs的结果长度设定
int i = 0;
while(rs.next()){
Map map=new HashMap();//
map.put("username",rs.getString(1));
map.put("sex",rs.getString(2));
maps[i++]=map;
//HashMap对象放入到数组当中
}
应该是Map[] maps = new HashMap[10];
另外 是哪一行报错?