Why is “while (rs.next())” necessary here?

前端 未结 5 1353
夕颜
夕颜 2021-01-07 16:08

I want to select the maximum line number from my database \"Logs\" and store it in a variable m.

Here\'s my code:

ResultSet rs          


        
5条回答
  •  执笔经年
    2021-01-07 16:37

    From the official documentation (which you should have read btw):

    Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

    You basically need to move it, in your case moving it once is enough:

     rs.next(); 
     System.out.println(rs.getInt("L")); 
    

提交回复
热议问题