Search code examples
javasqljdbcresultset

JDBC -- ResultSet values for column functions


How to get the outcome of the query-- the AVG(DIST).

MySQL is showing this result under the column "AVG(DIST)"-- no other clue.

How do i read this value from the ResultSet instance (rs in below code)?

PreparedStatement ps = c.prepareStatement("SELECT AVG(DIST) FROM POOL_TABLE");
ResultSet rs = ps.executeQuery();

ResultSet seems to be referring to them all by column names.

Not well-familiar to JDBC -- yet!

TIA.


Solution

  • Here it is:

    double avg = 0;
    while (rs.next()) {
        avg = rs.getDouble(1);
    }
    

    Use alias if you have many aggregate functions in your query. See below answer!