Question: Look at the starting source code. Run the program and see what it does. Currently it reads the basic country information from the COUNTRY table
- Look at the starting source code.
- Run the program and see what it does. Currently it reads the basic country information from the COUNTRY table of the database and prints information on the first country only.
- Start by updating the Main class so that it prints information on all the countries, not just the first one.
- 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.
- Update Main to print the languages for each country.
- Hint: This project parallels the project used in the JDBC Tutorial. The last part of the tutorial shows how to implement code to read email addresses for each customer. You can do something similar to read languages for each country.
- Use good coding style & design and follow standard Java conventions.
- Test your code as you go.
- Document your changes.
- For any changed classes, be sure your name is in the @author list and update the version.
- As you add and update methods make sure each method has a comment header that correctly describes its behavior.
the code is
The following is the Main Class:
main.java
import java.util.List; /** * Read from the Country database and print data on the countries * @author Cara Tang */ public class Main { public static void main(String[] args) { CountryDB cdb = new CountryDB(); List countries = cdb.getCountries(); Country firstCountry = countries.get(0); System.out.println("First country:"); System.out.println("Name: " + firstCountry.getName() + " Population: " + firstCountry.getPopulation() + " Median Age: " + firstCountry.getMedianAge() + " Coastline: " + firstCountry.getCoastlineKm() + "km"); } }
The following is the country Class:
country.java
/** * A country in the world * @author Cara Tang */ public class Country { private int id; private String name; private long population; private double medianAge; private long coastlineKm; /** * Create a Country object with the given properties */ public Country(int id, String name, long population, double medianAge, long coastlineKm) { this.id = id; this.name = name; this.population = population; this.medianAge = medianAge; this.coastlineKm = coastlineKm; } public int getId() { return id; } public String getName() { return name; } public long getPopulation() { return population; } public double getMedianAge() { return medianAge; } public long getCoastlineKm() { return coastlineKm; } }
The following is the CountryDB Class:
countryDB.java
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 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(); } /** * @return list of countries read from the country database */ public List getCountries() { return countries; } /** * 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"), rs.getLong("CoastlineKm"))); } } catch (SQLException e) { System.err.println("ERROR: " + e.getMessage()); e.printStackTrace(); } return countries; } }
please :
Need to know the final code for each Main.Java and countrDB.java and Country.Java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
