Question: Your primary task is to update the code so that the languages of each country are also read from the database and included in the

Your primary task is to update the code so that the languages of each country are also read from the database and included in the output. Languages are stored in a second table, COUNTRY_LANGUAGE, which has two columns: CountryId and Language. The tables COUNTRY and COUNTRY_LANGUAGE have a 1:M (one-to-many) relationship. One country can have many languages. For example, the country Canada has languages English and French. The CountryId column in COUNTRY_LANGUAGE is a foreign key to the COUNTRY table.

Update the project to read the languages. You can follow these steps:

Update the Country class so that it can store a list of languages. You need to use a collection because a country can have more than 1 language. You'll need to add a field for the list, a getter, and a method that allows you to add a language to the collection (e.g., addLanguage(String language))

Update CountryDB to read the languages. You may find the tutorial page on PreparedStatement helpful.

Update Main to print the languages for each country.

The following is the Main Class:

package edu.pcc.cis233j.countries; import java.util.List; /**  * Read from the Country database and print data on the countries  * @author Your Name  */ public class Main { public static void main(String[] args) { CountryDB cdb = new CountryDB(); List countries = cdb.getCountries(); /**  * Modified code to use a loop to get all countries population and age to display  */  Country firstCountry = countries.get(0); for (Country country : countries) { System.out.println("Country:"); System.out.println("Name: " + country.getName() + " Population: " + country.getPopulation() + " Median Age: " + country.getMedianAge()); } }} 

The following is the country Class

package edu.pcc.cis233j.countries; /**  * A country in the world  * @author Your Name & Cara Tang  */ public class Country { private int id; private String name; private long population; private double medianAge; /**  * Create a Country object with the given properties  */  public Country(int id, String name, long population, double medianAge) { this.id = id; this.name = name; this.population = population; this.medianAge = medianAge; } public int getId() { return id; } public String getName() { return name; } public long getPopulation() { return population; } public double getMedianAge() { return medianAge; } } 

The following is the CountryDB Class

package edu.pcc.cis233j.countries; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /**  * Read data from the Countries database  * @author Your Name & Cara Tang  */ public class CountryDB { private static final String DB_NAME = "Countries"; private static final String DB_URL = "jdbc:jtds:sqlserver://cisdbss.pcc.edu/" + DB_NAME; private static final String USERNAME = "233jstudent"; private static final String PASSWORD = "tnedutsj332"; private static final String GET_COUNTRIES_SQL = "SELECT * FROM COUNTRY"; private List countries; /**  * Create a CountryDB object  * Read from the Countries database and populate the countries list  */  public CountryDB() { countries = readCountries(); } /**  * Create and return a connection to the database  * @return connection to the countries database  */  private Connection getConnection() throws SQLException { Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD); return connection; } /**  * Read country info from the Country table.  * If an error occurs, a stack trace is printed to standard error and an empty list is returned.  * @return the list of countries read  */  private List readCountries() { List countries = new ArrayList<>(); try ( Connection connection = getConnection(); PreparedStatement stmt = connection.prepareStatement(GET_COUNTRIES_SQL); ResultSet rs = stmt.executeQuery() ) { while (rs.next()) { countries.add(new Country(rs.getInt("Id"), rs.getString("Name"), rs.getLong("Population"), rs.getDouble("MedianAge"))); } } catch (SQLException e) { System.err.println("ERROR: " + e.getMessage()); e.printStackTrace(); } return countries; } /**  * @return list of countries read from the country database  */  public List getCountries() { return countries; } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!