我的目标是从我的Country类调用我的对象方法getId,并且我需要在readCountryLanguages sql语句之前在我的国家/地区列表上插入一个循环。
public class ReadCountryDB {
private static final String DB_NAME = "Countries";
private static final String DB_URL = "jdbc:jtds:sqlserver://cisdbss.***.***/" + DB_NAME;
private static final String COUNTRY_TABLE_NAME = "COUNTRY";
private static final String USERNAME = "233jExample";
private static final String PASSWORD = "tnedutsj332";
private List<Country> countries;
/**
* Create a ReadCountryDB object
* Read from the Country database and populate the countries list
*/
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);
readCountryLanguages(sqlStatement, countries);//<--added
} catch (SQLException e) {
System.err.println("ERROR: " + e.getMessage());
e.printStackTrace();
} finally {
close(sqlStatement);
close(connection);
}
}
/**
* Read country info from the Country table
*
* @param sqlStatement
* @return the list of countries read
* @throws SQLException
*/
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;
}
//add country list loop here
private List<String> readCountryLanguages(Statement sqlStatement, List<Country>countries) throws SQLException {
List<String> languages = new ArrayList<>();
ResultSet resultSet = sqlStatement.executeQuery("SELECT language FROM COUNTRY_LANGUAGE");
while (resultSet.next()) {
String language = new String(resultSet.getString("Languages"));
int getId = new getId();//call getId method here
}
return languages;
}
/**
* Close the given statement, eat any errors
*
* @param sqlStatement
*/
private void close(Statement sqlStatement) {
if (sqlStatement != null) {
try {
sqlStatement.close();
} catch (SQLException e) {
}
}
}
/**
* Close the given connection, eat any errors.
*
* @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;
}
}以下是我试图调用的对象方法的Country类。
import java.util.ArrayList;
import java.util.List;
public class Country {
private int id;
private String name;
private long population;
private double medianAge;
private static List<String> languages;
/**
* 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;
Country.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;
}
}
答案 0 :(得分:0)
您的代码看起来应该是这样的:
private void readCountryLanguages(Statement sqlStatement, List<Country> countries) throws SQLException {
for (Country country : countries) {
ResultSet resultSet = sqlStatement.executeQuery("SELECT language FROM COUNTRY_LANGUAGE WHERE country_id=" + country.getId());
if (resultSet.next()) {
//todo here for example:
String[] langs = ....;' //get the langs from the resultSet (depending on your database)
for(String lang : langs){
country.addLanguage(lang);
}
}
resultSet.close();
}
}
然后你可以通过调用你的函数来获取语言
List<String> languages = yourCountry.getLanguages();