不懂楼主的意思.
具体你的Sql语句与JDBC无关,你查询完,返回一个ResultSet对象.
这时你new一个你所要返回的对象,比如:public class Test(){
private String bName;
private int sum;
get...set....methods
}
然后:
rs = stm.executeQuery(sql);//运行查询,并取结果集
List resultList =null;
if(rs != null){//判断结果集是否为空
resultList = new ArrayList();//new一个List
 while(rs.next()){//循环取结果集,类似游标的作用
 Test test = new Test();//你所要用来包装结果的对象
 test.setBName(rs.getString("bname"));//从结果集中取出bname的值,类型为String
 test.setSum(rs.getInt("sum"));//从结果集中取出sum的值,类型为int
 resultList.add(test);//将对象保存到List中
 }
}return resultList;//你要的结果对象List

解决方案 »

  1.   

    这是我现在用的方法:
      public Collection getDetailCount(String sql) throws Exception {
        Collection colValue = new ArrayList();
        try{
          con = DbAccess.getInstance().getCon();
          Statement stm = con.createStatement();
          ResultSet rs = stm.executeQuery(sql);
          while(rs.next()){
            String typeName = rs.getString(1);
            float accValue = rs.getFloat(2);
            AccTypeCount atc = new AccTypeCount();
            atc.setTypeName(typeName);
            atc.setAccValue(accValue);
            colValue.add(atc);
          }
          rs.close();
          stm.close();
          con.close();
        }
        catch(Exception e){
          e.printStackTrace();
        }
        return colValue;
      }
    就是把结果保存在Collection中,其中AccTypeCount是我为了保存结果而加的Bean,这个方法好吗,有没有更好的方法?
      

  2.   

    There 's no problem with your solution.but some blemish in your codes.public Collection getDetailCount(String sql) throws Exception {
        Collection colValue = new ArrayList();
        try{
          con = DbAccess.getInstance().getCon();
          Statement stm = con.createStatement();
          ResultSet rs = stm.executeQuery(sql);
          while(rs.next()){
            String typeName = rs.getString(1);
            float accValue = rs.getFloat(2);
            AccTypeCount atc = new AccTypeCount();
            atc.setTypeName(typeName);
            atc.setAccValue(accValue);
            colValue.add(atc);
          }    }
        catch(Exception e){
          e.printStackTrace();
        }finally{
          try{
            rs.close();
            stm.close();
            con.close();
          }
          catch(SQLException ex){}
        }
        return colValue;
      }Usually,we put 'close' method in finally block. And ,rs perhaps could be null sometime.So I suggest that you should create another method to close your rs,stm and conn.
      

  3.   

    ResultSet转进Collection,然后放入session传递