将ResultSet转为List
2015-11-13 00:05
295 查看
/article/6080079.html
public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {
if (rs == null)
return Collections.EMPTY_LIST;
ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等
int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数
List list = new ArrayList();
Map rowData = new HashMap();
while (rs.next()) {
rowData = new HashMap(columnCount);
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
System.out.println("list:" + list.toString());
}
return list;
}
接着在其他方法里处理返回的List
List ls = resultSetToList(rs);
Iterator it = ls.iterator();
while(it.hasNext()) {
Map hm = (Map)it.next();
System.out.println(hm.get("字段名大写"));
}
分类: Java
好文要顶 关注我 收藏该文
seaven
关注 - 0
粉丝 - 10
+加关注
1
0
(请您对文章做出评价)
« 上一篇:Java
Timer
» 下一篇:Java调用C#的web
Service
public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {
if (rs == null)
return Collections.EMPTY_LIST;
ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等
int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数
List list = new ArrayList();
Map rowData = new HashMap();
while (rs.next()) {
rowData = new HashMap(columnCount);
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
System.out.println("list:" + list.toString());
}
return list;
}
接着在其他方法里处理返回的List
List ls = resultSetToList(rs);
Iterator it = ls.iterator();
while(it.hasNext()) {
Map hm = (Map)it.next();
System.out.println(hm.get("字段名大写"));
}
分类: Java
好文要顶 关注我 收藏该文
seaven
关注 - 0
粉丝 - 10
+加关注
1
0
(请您对文章做出评价)
« 上一篇:Java
Timer
» 下一篇:Java调用C#的web
Service
相关文章推荐
- Oracle复杂查询
- 如何组织构建多文件 C 语言程序(二)
- 超常用的PHP正则表达式收集整理
- 15个超实用的php正则表达式
- python开发之文件操作用法实例
- python编程开发之类型转换convert实例分析
- python编程开发之日期操作实例分析
- python编程开发之textwrap文本样式处理技巧
- Python编程入门之Hello World的三种实现方式
- Python的Flask框架的简介和安装方法
- Python下的常用下载安装工具pip的安装方法
- 深入解析Python中的urllib2模块
- Python设置Socket代理及实现远程摄像头控制的例子
- 使用Python下载歌词并嵌入歌曲文件中的实现代码
- Python的pycurl包用法简介
- 简单了解Python下用于监视文件系统的pyinotify包
- Python下rrdtool模块的基本使用方法
- 浅析Python中signal包的使用
- PHP基于yii框架实现生成ICO图标
- 合格的PHP程序员必备技能