Reputation: 155
In this java project I having issues displaying the languages that I pulled from a database and stored in a ArrayList to read in my readCountry class. My question why wont it display languages and why does it tell me my readCountryLanguages method in not locally used?
import java.util.ArrayList;
import java.util.List;
public class Country {
private int id;
private String name;
private long population;
private double medianAge;
private List<String> languages;
/**
* Country Class
*/
public Country(int id, String name, long population, double medianAge) {
this.id = id;
this.name = name;
this.population = population;
this.medianAge = medianAge;
this.languages = new ArrayList<>();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getMedianAge() {
return medianAge;
}
public void addLanguage (String language) {
languages.add(language);
}
public List<String> getLanguages() {
return languages;
}
}
public class ReadCountryDB {
private static final String DB_NAME = "Countries";
private static final String
private static final String COUNTRY_TABLE_NAME = "COUNTRY";
private static final String USERNAME = "2bjExample";
private static final String PASSWORD = "tnedutsh332";
private List<Country> countries;
public ReadCountryDB() {
Connection connection = null;
Statement sqlStatement = null;
try {
connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
sqlStatement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
countries = readCountries(sqlStatement);
} catch (SQLException e) {
System.err.println("ERROR: " + e.getMessage());
e.printStackTrace();
} finally {
close(sqlStatement);
close(connection);
}
}
private List<Country> readCountries(Statement sqlStatement) throws SQLException {
List<Country> countries = new ArrayList<Country>();
ResultSet resultSet = sqlStatement.executeQuery("SELECT * FROM " + COUNTRY_TABLE_NAME);
while (resultSet.next()) {
countries.add(new Country(resultSet.getInt("Id"),
resultSet.getString("Name"),
resultSet.getLong("Population"),
resultSet.getDouble("MedianAge")));
}
return countries;
}
private List<String>readCountryLanguages(Statement sqlStatement) throws SQLException {
List<String> languages = new ArrayList<>();
ResultSet resultSet = sqlStatement.executeQuery("SELECT language FROM COUNTRY_LANGUAGE");
while (resultSet.next()) {
languages.add(new String(resultSet.getString("CountryID")));
}
return languages;
}
/**
*
* @param sqlStatement
*/
private void close(Statement sqlStatement) {
if (sqlStatement != null) {
try {
sqlStatement.close();
} catch (SQLException e) {
}
}
}
/**
*
* @param connection
*/
private void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
/**
* @return list of countries read from the country database
*/
public List<Country> getCountries() {
return countries;
}
}
import java.util.List;
/**
* CountryMain class
*/
public class CountryMain {
public static void main(String[] args) {
ReadCountryDB rcdb = new ReadCountryDB();
List<Country> countries = rcdb.getCountries();
for (Country firstCountry : countries) {
System.out.println("First country:");
System.out.println("Name: " + firstCountry.getName()
+ " Population: " + firstCountry.getPopulation()
+ " Language: " + firstCountry.getLanguages()
+ " Median Age: " + firstCountry.getMedianAge());
}
}
}
Upvotes: 0
Views: 10956
Reputation: 522741
Based on the code which you posted, the readCountryLanguages()
method never gets called inside Country.java
. Your IDE is alerting you of this, since you generally don't want to be building .class
files containing code you are not using.
The readCountryLanguages()
method also appears to be where you obtain your list of languages from the database. You probably wanted to call this method at some point but forgot to.
Upvotes: 2