@@ -61,57 +61,7 @@ public void disconnect() {
6161 }
6262 }
6363
64- /**
65- * Gets all cities and sorts them by population in descending order
66- * returns a list of cities sorted by population in descending order, or return null if thrown exception
67- */
68-
69- public ArrayList <City > getAllCities () {
70- try {
71- // Create an SQL statement
72- Statement stmt = con .createStatement ();
73-
74- // Modified SQL query to get all cities with their country and population
75- String strSelect =
76- "SELECT city.Name AS CityName, " +
77- "country.Name AS Country, " +
78- "city.Population AS Population " +
79- "FROM city " +
80- "JOIN country ON city.CountryCode = country.Code " +
81- "ORDER BY city.Population DESC" ;
82-
83- // Execute SQL statement
84- ResultSet rset = stmt .executeQuery (strSelect );
85-
86- // Extract city information
87- ArrayList <City > cities = new ArrayList <City >();
88- while (rset .next ()) {
89- City city = new City ();
90- city .name = rset .getString ("CityName" );
91- city .country = rset .getString ("Country" );
92- city .population = rset .getInt ("Population" );
93-
94- cities .add (city );
95- }
9664
97- return cities ;
98-
99- } catch (Exception e ) {
100- System .out .println (e .getMessage ());
101- System .out .println ("Failed to get the cities" );
102- return null ;
103- }
104-
105- }
106- public void Display (List <City > cities ) {
107- if (cities != null && !cities .isEmpty ()) {
108- for (City c : cities ) {
109- System .out .println ("City: " + c .name + ", Country: " + c .country + ", Population: " + c .population );
110- }
111- } else {
112- System .out .println ("No capital cities found." );
113- }
114- }
11565 public static void main (String [] args )
11666 {
11767 // Create new Application
@@ -121,11 +71,11 @@ public static void main(String[] args)
12171 a .connect ();
12272
12373 // Extract city information
124- ArrayList <City > cityList = a .getAllCities ();
125- a .Display (cityList );
74+ PrintCityValues printCityValues = new PrintCityValues ();
75+ printCityValues .getAllCities (a .con );
76+
12677
12778
128- System .out .println (cityList .size ());
12979
13080 // Disconnect from database
13181 a .disconnect ();
0 commit comments