in java project having issues displaying languages pulled database , stored in arraylist read in readcountry class. question why wont display languages , why tell me 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(); } { close(sqlstatement); close(connection); } } private list<country> readcountries(statement sqlstatement) throws sqlexception { list<country> countries = new arraylist<country>(); resultset resultset = sqlstatement.executequery("select * " + 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 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 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(); (country firstcountry : countries) { system.out.println("first country:"); system.out.println("name: " + firstcountry.getname() + " population: " + firstcountry.getpopulation() + " language: " + firstcountry.getlanguages() + " median age: " + firstcountry.getmedianage()); } }
}
based on code posted, readcountrylanguages()
method never gets called inside country.java
. ide alerting of this, since don't want building .class
files containing code not using.
the readcountrylanguages()
method appears obtain list of languages database. wanted call method @ point forgot to.
Comments
Post a Comment