I’m beginning to learn about connecting to databases via Java programs. I have a table called City, which contains just two columns: CityName and Population. I want to find the highest population, and then give that population along with the name of the corresponding city.
I know how to get just the highest population (see the following, which I know is probably not ideal but at least is working for me):
double max = 0.0;
String sqlStatement = "SELECT MAX(Population) FROM City";
ResultSet result = stmt.executeQuery(sqlStatement);
if (result.next())
{
highest = result.getDouble(1);
}
System.out.printf("Highest population: %,.0f", highest);
I am pretty sure that to get the highest population PLUS the name of the corresponding city, my Sql statement should eb changed to :
"SELECT CityName, MAX(Population) FROM City GROUP BY CityName";
But I can’t get it to work beyond that. I thought I could add something like
String highestCity = result.getString(2)
and then add highestCity to the System.out.printf output line, but that isn’t working.
Can anyone help?
Thanks!
How about this ?