How to get row count using ResultSet in Java?

前端 未结 13 2064
[愿得一人]
[愿得一人] 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:46

    Following two options worked for me:

    1) A function that returns the number of rows in your ResultSet.

    private int resultSetCount(ResultSet resultSet) throws SQLException{
        try{
            int i = 0;
            while (resultSet.next()) {
                i++;
            }
            return i;
        } catch (Exception e){
           System.out.println("Error getting row count");
           e.printStackTrace();
        }
        return 0;
    }
    

    2) Create a second SQL statement with the COUNT option.

    0 讨论(0)
提交回复
热议问题