如何把resultset结果集转换成list集合
1个回答
展开全部
方法一:
ResultSet转换为List的方法
private static List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); //Map rowData;
while (rs.next()) { //rowData = new HashMap(columnCount);
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
} return list;
}
方法二:
/**
* 通用取结果方案,返回list
*
* @param rs
* @return
* @throws SQLException
*/
public List extractData(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int num = md.getColumnCount();
List listOfRows = new ArrayList();
while (rs.next()) {
Map mapOfColValues = new HashMap(num);
for (int i = 1; i <= num; i++) {
mapOfColValues.put(md.getColumnName(i), rs.getObject(i));
}
listOfRows.add(mapOfColValues);
}
return listOfRows;
}
/**
* 通用取结果方案,返回JSONArray
*
* @param rs
* @return
* @throws SQLException
*/
public JSONArray extractJSONArray(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int num = md.getColumnCount();
JSONArray array = new JSONArray();
while (rs.next()) {
JSONObject mapOfColValues = new JSONObject();
for (int i = 1; i <= num; i++) {
mapOfColValues.put(md.getColumnName(i), rs.getObject(i));
}
array.add(mapOfColValues);
}
return array;
}
ResultSet在的二纬结构可以用二维模型处理,即网格数据 处理这里模型时候,通常用list与与Map的合成,json语法格式本质上是map格式,所以在数据与java,js处理上可以做如上的抽取.
方法三:
原理就是用Map存放单条数据然后放到List里
用起来可以这样调用 List list = rs.selectRS(sqlStr);
for(int i=0;i<list.size;i++)
{
((Map)list.get(i)).get("username");
}
这样的话就不用每次在写数据库连接了,而且有比较复杂的页面格式输出就可以通过对 i 的控制来实现了
下面是具体实现类可以结合struts,webwork2的标签来用,我已经测试过了没问题
我也看到一些人是自己写类来代替Map来用的
package com;
import conn.DBConnManager;
import java.sql.*;
import java.util.*;
public class ResultGather
{
private String sql;
public ResultGather()
{
}
public ResultGather(String sqlcom)
{
this.sql=sqlcom;
}
public List selectRS(String sqlStr)
{
this.sql = sqlStr;
return selectRS();
}
public List selectRS()
{
List rsall = new ArrayList();
Map rsTree;
DBConnManager conn = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = DBConnManager.getInstance();
con = conn.getConnection("mssql");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();//取数据库的列名 我觉得名比1,2,3..更好用
int numberOfColumns = rsmd.getColumnCount();
while(rs.next())
{
rsTree = new HashMap(numberOfColumns);//注意要new
for(int r=1;r<numberOfColumns+1;r++)
{
rsTree.put(rsmd.getColumnName(r),rs.getObject(r));
}
rsall.add(rsTree);
}
}catch(java.lang.Exception ex){
ex.printStackTrace();
}finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.releaseConnection("mssql",con);
}catch(Exception e){
}
}
return rsall;
}
}
简单的东西 交流交流
评论:自己写什么类哦,用spring的jdbc模板干脆
List lst=jdbcTemplate.queryForList(list);
方法四:
将ResultSet 转成Map<String, List<String> >:
public Map<String, ArrayList<String> > query(String sql){
Map<String, ArrayList<String> > map = new HashMap<String, ArrayList<String> >();
PreparedStatement preStat = null;
try {
preStat = conn.prepareStatement(sql);
ResultSet rs = preStat.executeQuery();
ResultSetMetaData rsMeta = rs.getMetaData();
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
map.put(rsMeta.getColumnName(i+1), new ArrayList<String>());
}
while(rs.next()){
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
String columnName = rsMeta.getColumnName(i+1);
map.get(columnName).add(rs.getString(columnName));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
preStat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Map<String, ArrayList<String> > query(String sql){
Map<String, ArrayList<String> > map = new HashMap<String, ArrayList<String> >();
PreparedStatement preStat = null;
try {
preStat = conn.prepareStatement(sql);
ResultSet rs = preStat.executeQuery();
ResultSetMetaData rsMeta = rs.getMetaData();
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
map.put(rsMeta.getColumnName(i+1), new ArrayList<String>());
}
while(rs.next()){
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
String columnName = rsMeta.getColumnName(i+1);
map.get(columnName).add(rs.getString(columnName));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
preStat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ResultSet转换为List的方法
private static List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); //Map rowData;
while (rs.next()) { //rowData = new HashMap(columnCount);
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
} return list;
}
方法二:
/**
* 通用取结果方案,返回list
*
* @param rs
* @return
* @throws SQLException
*/
public List extractData(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int num = md.getColumnCount();
List listOfRows = new ArrayList();
while (rs.next()) {
Map mapOfColValues = new HashMap(num);
for (int i = 1; i <= num; i++) {
mapOfColValues.put(md.getColumnName(i), rs.getObject(i));
}
listOfRows.add(mapOfColValues);
}
return listOfRows;
}
/**
* 通用取结果方案,返回JSONArray
*
* @param rs
* @return
* @throws SQLException
*/
public JSONArray extractJSONArray(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int num = md.getColumnCount();
JSONArray array = new JSONArray();
while (rs.next()) {
JSONObject mapOfColValues = new JSONObject();
for (int i = 1; i <= num; i++) {
mapOfColValues.put(md.getColumnName(i), rs.getObject(i));
}
array.add(mapOfColValues);
}
return array;
}
ResultSet在的二纬结构可以用二维模型处理,即网格数据 处理这里模型时候,通常用list与与Map的合成,json语法格式本质上是map格式,所以在数据与java,js处理上可以做如上的抽取.
方法三:
原理就是用Map存放单条数据然后放到List里
用起来可以这样调用 List list = rs.selectRS(sqlStr);
for(int i=0;i<list.size;i++)
{
((Map)list.get(i)).get("username");
}
这样的话就不用每次在写数据库连接了,而且有比较复杂的页面格式输出就可以通过对 i 的控制来实现了
下面是具体实现类可以结合struts,webwork2的标签来用,我已经测试过了没问题
我也看到一些人是自己写类来代替Map来用的
package com;
import conn.DBConnManager;
import java.sql.*;
import java.util.*;
public class ResultGather
{
private String sql;
public ResultGather()
{
}
public ResultGather(String sqlcom)
{
this.sql=sqlcom;
}
public List selectRS(String sqlStr)
{
this.sql = sqlStr;
return selectRS();
}
public List selectRS()
{
List rsall = new ArrayList();
Map rsTree;
DBConnManager conn = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = DBConnManager.getInstance();
con = conn.getConnection("mssql");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();//取数据库的列名 我觉得名比1,2,3..更好用
int numberOfColumns = rsmd.getColumnCount();
while(rs.next())
{
rsTree = new HashMap(numberOfColumns);//注意要new
for(int r=1;r<numberOfColumns+1;r++)
{
rsTree.put(rsmd.getColumnName(r),rs.getObject(r));
}
rsall.add(rsTree);
}
}catch(java.lang.Exception ex){
ex.printStackTrace();
}finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.releaseConnection("mssql",con);
}catch(Exception e){
}
}
return rsall;
}
}
简单的东西 交流交流
评论:自己写什么类哦,用spring的jdbc模板干脆
List lst=jdbcTemplate.queryForList(list);
方法四:
将ResultSet 转成Map<String, List<String> >:
public Map<String, ArrayList<String> > query(String sql){
Map<String, ArrayList<String> > map = new HashMap<String, ArrayList<String> >();
PreparedStatement preStat = null;
try {
preStat = conn.prepareStatement(sql);
ResultSet rs = preStat.executeQuery();
ResultSetMetaData rsMeta = rs.getMetaData();
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
map.put(rsMeta.getColumnName(i+1), new ArrayList<String>());
}
while(rs.next()){
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
String columnName = rsMeta.getColumnName(i+1);
map.get(columnName).add(rs.getString(columnName));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
preStat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Map<String, ArrayList<String> > query(String sql){
Map<String, ArrayList<String> > map = new HashMap<String, ArrayList<String> >();
PreparedStatement preStat = null;
try {
preStat = conn.prepareStatement(sql);
ResultSet rs = preStat.executeQuery();
ResultSetMetaData rsMeta = rs.getMetaData();
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
map.put(rsMeta.getColumnName(i+1), new ArrayList<String>());
}
while(rs.next()){
for(int i = 0; i < rsMeta.getColumnCount(); ++i){
String columnName = rsMeta.getColumnName(i+1);
map.get(columnName).add(rs.getString(columnName));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
preStat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询