Population Database   Compile and run CreateCityDB.java which will create a Java DB database named CityDB. The CityDB database will have a table named City, with the following columns: CityName & Population. Their Data Types: CHAR(50) Primary Key & DOUBLE. The CityName column stores the name of a city, and the Population column stores the population of that city. After you run the CreateCityDB.java program, the City table will contain 20 rows with various cities and their populations. Next, create an application that connects to the CityDB database and allows the user to select any of the following operations: Sort the list of cities by population, in ascending order. Sort the list of cities by population, in descending order. Sort the list of cities by name. Get the total population of all the cities. Get the average population of all the cities. Get the highest population. Get the lowest population.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

Population Database

 

Compile and run CreateCityDB.java which will create a Java DB database named CityDB. The CityDB database will have a table named City, with the following columns: CityName & Population. Their Data Types: CHAR(50) Primary Key & DOUBLE. The CityName column stores the name of a city, and the Population column stores the population of that city. After you run the CreateCityDB.java program, the City table will contain 20 rows with various cities and their populations. Next, create an application that connects to the CityDB database and allows the user to select any of the following operations:

  • Sort the list of cities by population, in ascending order.
  • Sort the list of cities by population, in descending order.
  • Sort the list of cities by name.
  • Get the total population of all the cities.
  • Get the average population of all the cities.
  • Get the highest population.
  • Get the lowest population.

 

CreateCityDB.java

import java.sql.*;

/**
  This program creates the CityDB database.                                    *
*/
 
public class CreateCityDB
{
   public static void main(String[] args) throws Exception
   {
        String sql;
      final String DB_URL = "jdbc:derby:CityDB;create=true";
      
      // Create a connection to the database.
      Connection conn = DriverManager.getConnection(DB_URL);
            
      // Create a Statement object.
      Statement stmt = conn.createStatement();
            
      try
      {    
                     
         // Create the City table.
         System.out.println("Creating the City table...");
         stmt.execute("CREATE TABLE City ("    +
                      "CityName CHAR(25) NOT NULL PRIMARY KEY, "   +
                      "Population DOUBLE)");
                             
         // Add some rows to the new table.
         sql = "INSERT INTO City VALUES" +
               "('Beijing', 12500000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Buenos Aires', 13170000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Cairo', 14450000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Calcutta', 15100000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Delhi', 18680000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Jakarta', 18900000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Karachi', 11800000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Lagos', 13488000)";
         stmt.executeUpdate(sql);
            
         sql = "INSERT INTO City VALUES" +
               "('London', 12875000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Los Angeles', 15250000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Manila', 16300000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Mexico City', 20450000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Moscow', 15000000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Mumbai', 19200000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('New York City', 19750000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Osaka', 17350000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Sao Paulo', 18850000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Seoul', 20550000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Shanghai', 16650000)";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO City VALUES" +
               "('Tokyo', 32450000)";
         stmt.executeUpdate(sql);
      }
      catch(Exception ex)
      {
         System.out.println("ERROR: " + ex.getMessage());
      }
      finally
      {
         // Close the resources.
         stmt.close();     
         conn.close();
         System.out.println("Done");
      }
   } 
}

 

Population Database
Beijing
Buenos Aires
Cairo
Calcutta
Delhi
Jakarta
Karachi
Lagos
London
Los Angeles
Manila
Mexico City
Moscow
Mumbai
New York City
Osaka
Sao Paulo
Seoul
Shanghai
Tokyo
CITYNAME
Sort Ascending
Get Average
1
// Course:
2
// File:
3 // Written by:
4 // Written on:
12,500,000
13,170,000
14,450,000
15,100,000
18,680,000
18,900,000
11,800,000
13,488,000
12,875,000
15,250,000
16,300,000
20,450,000
15,000,000
19,200,000
19,750,000
17,350,000
18,850,000
20,550,000
16,650,000
32,450,000
Sort Descending
Get Highest
your name
8 May 2023
POPULATION
CIS 228 Java Programming II
PopulationDemo.java
Sort Name
Get Lowest
The first lines in every source file that you write should consist of a comment block as follows:
X
Get Total
Exit
Transcribed Image Text:Population Database Beijing Buenos Aires Cairo Calcutta Delhi Jakarta Karachi Lagos London Los Angeles Manila Mexico City Moscow Mumbai New York City Osaka Sao Paulo Seoul Shanghai Tokyo CITYNAME Sort Ascending Get Average 1 // Course: 2 // File: 3 // Written by: 4 // Written on: 12,500,000 13,170,000 14,450,000 15,100,000 18,680,000 18,900,000 11,800,000 13,488,000 12,875,000 15,250,000 16,300,000 20,450,000 15,000,000 19,200,000 19,750,000 17,350,000 18,850,000 20,550,000 16,650,000 32,450,000 Sort Descending Get Highest your name 8 May 2023 POPULATION CIS 228 Java Programming II PopulationDemo.java Sort Name Get Lowest The first lines in every source file that you write should consist of a comment block as follows: X Get Total Exit
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Query Syntax
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education