How to get row count using ResultSet in Java?

前端 未结 13 2165
[愿得一人]
[愿得一人] 2020-11-27 13:05

I\'m trying to create a simple method that receives a ResultSet as a parameter and returns an int that contains the row count of the ResultSet. Is this a valid way of doing

13条回答
  •  天命终不由人
    2020-11-27 13:26

    The ResultSet has it's methods that move the Cursor back and forth depending on the option provided. By default, it's forward moving(TYPE_FORWARD_ONLY ResultSet type). Unless CONSTANTS indicating Scrollability and Update of ResultSet properly, you might end up getting an error. E.g. beforeLast() This method has no effect if the result set contains no rows. Throws Error if it's not TYPE_FORWARD_ONLY.

    The best way to check if empty rows got fetched --- Just to insert new record after checking non-existence

    if( rs.next() ) {
    
       Do nothing
    } else {
      No records fetched!
    }
    

    See here

提交回复
热议问题