Avoiding skipping a row By next() Method of ResultSet

后端 未结 6 1996
野性不改
野性不改 2021-01-15 00:34

This is a simple code print some rows from a Database. but When I execute this nothing is print on screen. I figured that the problem is rs.next() method is ski

6条回答
  •  感情败类
    2021-01-15 01:10

    rs.next() will increment cursor in if so if resultset has only one row returned by query then while will move cursor again and no data will get print.Use do..while loop instead.

    if (rs.next()) {
                do {
                    System.out.print(rs.getString("idUser") + " ,");
                    System.out.print(rs.getString("Name") + " ,");
                    System.out.print(rs.getString("Email") + " ,");
                    System.out.println(rs.getString("country") + " .");
                }while (rs.next());
            } 
    

提交回复
热议问题