/**
 * 转换 resultset 结果集为list
 * 
 * @param rs
 * @param propertyType
 * @return
 */
public List ReSultSetToList(ResultSet rs,int propertyType){

// 用来取得表列明的 对象
ResultSetMetaData rsmd = null;
List list = new ArrayList();
if(rs != null)
{
// 获取列对象
try {
rsmd = rs.getMetaData();
while(rs.next()){

// 获取传过来的对象实例
Map<String,Object> map = new HashMap<String,Object>();
// 获取列的属性及值
for(int i = 1 ; i <= rsmd.getColumnCount(); i ++)
{
// 获取列名
String columnName = rsmd.getColumnName(i);
if(columnName != null && !columnName.equalsIgnoreCase("NUMROW"))
{
if(propertyType ==0)
{
map.put(columnName.toUpperCase(), getValue(rs, rsmd, i));
}
else if(propertyType == 1)
{
map.put(columnName.toLowerCase(), getValue(rs, rsmd, i));
}
else
{
map.put(columnName, getValue(rs, rsmd, i));
}
}

}
if(map != null)
{
list.add(map);
}

         }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}

private Object getValue(ResultSet rs,ResultSetMetaData rsmd,int index)
{
int type = -9999;
try {
type = rsmd.getColumnType(index);
if(type == Types.INTEGER || type == Types.BIGINT || 
   type == Types.BIT ||  
   type == Types.SMALLINT 
)
{
return rs.getInt(index);
}
else if (type == Types.NUMERIC || type == Types.BIGINT)
{

return rs.getDouble(index);
}
else if(type == Types.DECIMAL || type == Types.DOUBLE)
{
return rs.getDouble(index);
}else if(type == Types.FLOAT || type == Types.DOUBLE){
return rs.getString(index);
}
else if (type == Types.DATE)
{
java.sql.Date date = rs.getDate(index);
java.sql.Time time = rs.getTime(index);
java.util.Date datetime = null;
if(date != null && time != null && !time.toString().startsWith("00:00"))
{
// datetime = new Date(date.getTime() + time.getTime());
datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date.toString()+" "+time.toString());
return DateConverter.toTimestamp(datetime);
}
else if(date != null)
{
datetime = new Date(date.getTime());
}
return datetime;
}
else if(type == Types.TIME)
{
return rs.getTime(index);
}
else if(type == Types.TIMESTAMP)
{
return rs.getTimestamp(index);
}
else
{
return rs.getString(index);

}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

解决方案 »

  1.   

    老兄,用Hiberbate多方便了,查询结果直接就是一个list
      

  2.   

    比如这样应用:
    public List queryDataList(jhdm,datatag){
    Connection con = null;
    ResultSet rset = null;
    PreparedStatement preStmt = null;
    try {
    BaseDao basedao = new BaseDao();
    con = basedao.getConnection();
    if (con != null) {
    con.setAutoCommit(false);
    preStmt = con.prepareStatement("select * from ZHLJ_ZHLJSJT where JHDM=? and ZHLJSJLX=?");
    preStmt.setString(1, jhdm);
    preStmt.setString(2, datatag);
    rset = preStmt.executeQuery();//查询得到的是 ResultSet 集合
                                    list = basedao.ReSultSetToList(rs,0);//调用basedao里的ReSultSetToList 方法将ResultSet 转换成List
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    try {
    con.rollback();
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    e.printStackTrace();
    }  catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    if (rset != null) {
    rset.close();
    rset = null;
    }
    if (preStmt != null) {
    preStmt.close();
    preStmt = null;
    }
    if (con != null)
    con.close();
    con = null;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    return tag;
    }
      

  3.   

    为了说明意思我 截取的代码片段,以上方法 返回的应该是list 
    public List queryDataList(jhdm,datatag){
    Connection con = null;
    List list = null;
            ResultSet rset = null;
            PreparedStatement preStmt = null;
            try {
                BaseDao basedao = new BaseDao();
                con = basedao.getConnection();
                if (con != null) {
                    con.setAutoCommit(false);
                    preStmt = con.prepareStatement("select * from ZHLJ_ZHLJSJT where JHDM=? and ZHLJSJLX=?");
                    preStmt.setString(1, jhdm);
                    preStmt.setString(2, datatag);
                    rset = preStmt.executeQuery();//查询得到的是 ResultSet 集合
                      list = basedao.ReSultSetToList(rs,0);//调用basedao里的ReSultSetToList 方法将ResultSet 转换成List
    } catch (SQLException e) {
                // TODO Auto-generated catch block
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }  catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (rset != null) {
                        rset.close();
                        rset = null;
                    }
                    if (preStmt != null) {
                        preStmt.close();
                        preStmt = null;
                    }
                    if (con != null)
                        con.close();
                    con = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }        }
            return list;
        }
      

  4.   

    这样的得到的list 可以直接通过强制类型转换成map
    Map map =(Map)list.get(i);
    map.get("列名");//来获取。ReSultSetToList(ResultSet rs,int propertyType)的propertyType指的的获取时用列名的大小写,@param propertyType 如果是Map对象,将封装大写还是小写:0.默认,大写 1.小写 其它.按数据库
      

  5.   

    比如说 我调用储存过程获取对一张表或者多长表的查询结果
    /**
        * 调用存储过程获list结果集数据 仅限于传入两个参数
        * @param jhdm 井号代码
        * @param parm1 参数二
        * @param ProcedureName 存储过程名
        * @return
        */
    public  List GetListByProcedure(String jhdm,Long parm1,String ProcedureName) {
    Session session = HibernateSessionFactory.getSession();
    List list = null;
    CallableStatement cstmt = null;
    ResultSet rset = null;
    Connection con = session.connection();
    String procedureQL = "{call "+ProcedureName+"(?,?,?)}";
    ResultSet rs = null;
    try {
    cstmt = con.prepareCall(procedureQL);
    cstmt.setString(1, jhdm);
    cstmt.setLong(2, parm1);
    cstmt.registerOutParameter(3,oracle.jdbc.OracleTypes.CURSOR);    
    cstmt.execute();
        rs = (ResultSet)cstmt.getObject(3); 
    list = ReSultSetToList(rs,0);
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    try {
    if (rset != null) {
    rset.close();
    rset = null;
    }
    if (con != null)
    con.close();
        con = null;
    } catch (SQLException e) {
    e.printStackTrace();
    }
    session.close();
    }
    return list;
    }
    假设这个存储过程返回的结果集里面字段名为 id,name,age,bz这4列
    上面调用的这个方法得到的list
    现在有另外个方法要对age年龄大60的找出来。
    对返回这个list
    public String getAgeOldThanSixty(List list){
    Map map = null; 
    String UserName="";
    if(list!=null&&list.size()>0)
    {for(int i=0;i<list.size();i++){
     map = (Map)list.get(i);
     if(Integer.parseInt(map.get("AGE")).toString()){
     UserName = map.get("NAME").toString();
     break;//为了给你说明问题 所以这里只找一个 
    }
    }
    }
    return UserName;
    }同样这个list可以不用任何处理的传到页面上去直接用
    strut1的 遍历器 和strut2的页面的和遍历相关的 s:select
    <s:iterator value="对应action里面返回页面的list对象名称" status="st"> 等直接的遍历
    字段名和数据库的相对应,大小写上面说过。
      

  6.   

    循环存进LIST中就可以了。。不用知道到底有多少条
      

  7.   

    主要是:
    DateConverter.toTimestamp(datetime);
    catch (ParseException e){}
    DateConverter和ParseException无法解释为类型。
    不知道少了什么包!
      

  8.   

    换成这样有问题吗?少了个getValue()方法。
    意思是性能方面会有区别吗?
    public List ReSultSetToList(ResultSet rs, int propertyType) {
    // 用来取得表列明的 对象
    ResultSetMetaData rsmd = null;
    List list = new ArrayList();
    if (rs != null) {
    // 获取列对象
    try {
    rsmd = rs.getMetaData();
    while (rs.next()) {
    // 获取传过来的对象实例
    Map<String, Object> map = new HashMap<String, Object>();
    // 获取列的属性及值
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
    // 获取列名
    String columnName = rsmd.getColumnName(i);
    if (columnName != null
    && !columnName.equalsIgnoreCase("NUMROW")) {
    if (propertyType == 0) {
    map.put(columnName.toUpperCase(), rs.getObject(i));
    } else if (propertyType == 1) {
    map.put(columnName.toLowerCase(), rs.getObject(i));
    } else {
    map.put(columnName,rs.getObject(i));
    }
    }
    }
    if (map != null) {
    list.add(map);
    }
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return list;
    }