can not retrieve all rows from the mysql database

advertisements

When am trying to retrieve all rows from database using java code am getting wrong output ie if table has 5 rows means the last row only displayed 5 times...

while(result.next()) {
            user = new User();
            user.setId(result.getInt("id"));
            user.setName(result.getString("name"));
            user.setPassword(result.getString("password"));

            list.add(user);
            result = result.next();
        }


Your Resultset logic is not correct, Resultset.next() works as cursor which move forward until to finish. Try

 while(result.next()) {
        user = new User();
        user.setId(result.getInt("id"));
        user.setName(result.getString("name"));
        user.setPassword(result.getString("password"));

        list.add(user);

    }