Avoiding skipping a row By next() Method of ResultSet

后端 未结 6 1990
野性不改
野性不改 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:16

    No no need to use :

     if (rs.next()) {
    

    The while is enough, this make the result two times. instead you can use :

    boolean b = false;
    while (rs.next()) {
       b = true;
       ...
    }
    
    if(!b) {
      JOptionPane.showMessageDialog(null, "Not Found");
    }
    

    You have to use PreparedStatement instead, to avoid any SQL Injection or syntax error.

提交回复
热议问题