日期:2014-05-20  浏览次数:20945 次

把结果集封装成list 遇到问题了 高手看看怎么办
Java code
public List<Admin> getAllAdmin() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Admin> adminList = new ArrayList<Admin>();
        int count = 0;
        try {
            conn = JdbcUtil.getConnection();
            String sql = "select id,adminid,adminname,password,time from admin";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            Admin[] adminArr = new Admin[]{};
            while (rs.next()) {
                //todo
            }
        }catch (SQLException e) {
            e.printStackTrace();    
        }
        return adminList;
    }


------解决方案--------------------
Java code

public List<Admin> getAllAdmin() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Admin> adminList = new ArrayList<Admin>();
        int count = 0;
        try {
            conn = JdbcUtil.getConnection();
            String sql = "select id,adminid,adminname,password,time from admin";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            
            while (rs.next()) {
              Admin admin = new Admin();
          admin.setId(rs.getString(1)) ;  
              admin.setAdminid(rs.getString(2)) ;  
              admin.setAdminname(rs.getString(3)) ;
              admin.setPassword(rs.getString(4)) ;  
              admin.setTime(rs.getString(5)) ;   
              adminList.add(admin);
            }
        }catch (SQLException e) {
            e.printStackTrace();    
        }
        return adminList;
    }