Search code examples
javajdbcresultset

How to get COUNT from ResultSet object?


I am passing the following query to a ResultSet object:

String query = "SELECT COUNT( DISTINCT KEY ), SOURCE FROM MY_TBL\n" +
            "GROUP BY SOURCE\n" +
            "ORDER BY SOURCE";

I want to capture the counts I am getting for each SOURCE and sum them into a total. How can I capture these counts via ResultSet since COUNT isn't a column name in the ResultSet and I don't think I can return it's value via rs.getInt("COUNT")?


Solution

  • getInt is overloaded, use index (an int) instead of a column name:

     rs.getInt(1); // the first column is 1