tags:

views:

29

answers:

1

how can i conver result set to list? i am using following code but its not working properly

private List<User> convertToList(ResultSet rs) {
        List<User> userList = new ArrayList();
        User user = new User();

        try {
            while (rs.next()) {

                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setFname(rs.getString("fname"));
                user.setLname(rs.getString("lname"));
                user.setUsertype(rs.getInt("usertype"));
                user.setPasswd(rs.getString("passwd"));
                user.setEmail(rs.getString("email"));
                userList.add(user);

            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnection();
        }

        return userList;

    }
+3  A: 

i am using following code but its not working properly

This doesn't describe the problem.

As a guess I would say that you only have one User object in your List. This is because you need to create a new User() object every time you read a new row from the ResultSet:

//User user = new User(); 

        try { 
            while (rs.next()) { 
                User user = new User(); 
                user.setId(rs.getInt("id")); 
                user.setUsername(rs.getString("username")); 
                user.setFname(rs.getString("fname")); 
                user.setLname(rs.getString("lname")); 
                user.setUsertype(rs.getInt("usertype")); 
                user.setPasswd(rs.getString("passwd")); 
                user.setEmail(rs.getString("email")); 
                userList.add(user); 

            } 
camickr