Search code examples
javasqlresultset

How to get count from resultset?


ResultSet rs = statement.executeQuery("SELECT COUNT(DISTINCT `id`) FROM `users`");

in MySQL i have answer "5" , how to get this 5 from resultset in java? int.


Solution

  • Get it as an int column :

    int count = -1;
    if (rs.next()) {
        count = rs.getInt(1);
    }