mardi 23 juin 2015

Same record is returned while there are different records in DB?

My code is returning object but same record multiple times because same object reference is been going through. Please suggest some modification so that i get all the records without repeating same rows.

DBConnection con = null;
        List<UserModel> users= new ArrayList<UserModel>();
        UserModel userModel = null;
        try {
            con = new DBConnection();
            LOGGER.info("############### DBUtils.getUsers() start");
            ResultSet rs = null;
            PreparedStatement pstmt = null;
            pstmt = con.connection.prepareStatement(
                    DBUtils.Select.USERS);
            pstmt.setString(1, user);
            pstmt.setString(2, pwd);

            rs = pstmt.executeQuery();
            while (rs.next()) {
                userModel = new UserModel();
                userModel.setName(rs.getString("NAME"));
                userModel.setUserId(rs.getString("USERID"));
                userModel.setPassword(rs.getString("PASSWORD"));
                userModel.setAccountDeleted(rs.getString("ACCOUNT_DELETED"));
                userModel.setAccountEnabled(rs.getString("ACCOUNT_ENABLED"));
                userModel.setAccountExpired(rs.getString("ACCOUNT_EXPIRED"));
                userModel.setCreationTime(rs.getString("CREATION_TIME"));
                userModel.setDeletionTime(rs.getString("DELETION_TIME"));
                userModel.setCredentialsExpired(rs
                        .getString("CREDENTIALS_EXPIRED"));
                userModel.setPermissions(rs.getString("PERMISSIONS"));
                userModel.setLastLoginTime(rs.getString("LAST_LOGIN_TIME"));
                LOGGER.info("############### DBUtils.getUsers() users : "+userModel);
                users.add(userModel);
            }

            LOGGER.info("############### DBUtils.getUsers() users : "+users);

        } catch (SQLException e) {
            e.printStackTrace();
        } 
        finally{
            if(con!= null){
                con.closeConnection();
            }
        }

Aucun commentaire:

Enregistrer un commentaire