As I process each country, how do I add only the languages with matching country id for that country?
在处理每个国家/地区时,如何仅添加具有该国家/地区匹配国家/地区ID的语言?
private List<String> readCountryLanguages(Statement sqlStatement, List<Country>countries) throws SQLException {
List<String> languages = new ArrayList<>();
for (Country country : countries ) {
ResultSet resultSet = sqlStatement.executeQuery("SELECT language FROM COUNTRY_LANGUAGE");
while (resultSet.next()) {
String language = new String(resultSet.getString("Languages"));
Country obj = new Country(0, language, 0, 0);
obj.getId();
}
}
return languages;
}
3 个解决方案
#1
Not sure about your table structure of COUNTRY_LANGUAGE but you can update the sql query to something like.
不确定您的COUNTRY_LANGUAGE的表结构,但您可以将sql查询更新为类似的。
SELECT language FROM COUNTRY_LANGUAGE WHERE country='" + country.getName() + "'
Here I am assuming you have a column "country" in your COUNTRY_LANGUAGE table and country name can be retrieve from Country object by calling Country.getName()
. But you can use the exact method from your actual implementation.
在这里,我假设您的COUNTRY_LANGUAGE表中有一个“country”列,并且可以通过调用Country.getName()从Country对象中检索国家/地区名称。但是您可以使用实际实现中的确切方法。
#2
-
return the Country id in a variable when you iterate; you should have something like
country.getId()
;迭代时返回变量中的Country id;你应该有像country.getId();
-
ResultSet resultSet = sqlStatement.executeQuery("SELECT language FROM COUNTRY_LANGUAGE where id='countryIdVariable'");
ResultSet resultSet = sqlStatement.executeQuery(“SELECT language FROM COUNTRY_LANGUAGE where id ='countryIdVariable'”);
-
change your sql query to be parametized/prepared statement so you can prevent sql injection and also add variable easier instead of having to write variable within single quote in your sql string.
将您的sql查询更改为参数化/预准备语句,这样您就可以防止sql注入并且还可以更容易地添加变量,而不必在sql字符串中的单引号内写入变量。
#3
AS menthion by Asura, First of all change your sql query that returns language where contryId is 'something'
作为Asura的AS menthion,首先改变你的sql查询,返回contryId是'某事'的语言
then in while loop add each language to languages list object by using add() method like this :
然后在while循环中使用add()方法将每种语言添加到语言列表对象,如下所示:
while (resultSet.next()) {
String language = resultSet.getString("Languages");
// add to list
languages.add(language);
}
}
#1
Not sure about your table structure of COUNTRY_LANGUAGE but you can update the sql query to something like.
不确定您的COUNTRY_LANGUAGE的表结构,但您可以将sql查询更新为类似的。
SELECT language FROM COUNTRY_LANGUAGE WHERE country='" + country.getName() + "'
Here I am assuming you have a column "country" in your COUNTRY_LANGUAGE table and country name can be retrieve from Country object by calling Country.getName()
. But you can use the exact method from your actual implementation.
在这里,我假设您的COUNTRY_LANGUAGE表中有一个“country”列,并且可以通过调用Country.getName()从Country对象中检索国家/地区名称。但是您可以使用实际实现中的确切方法。
#2
-
return the Country id in a variable when you iterate; you should have something like
country.getId()
;迭代时返回变量中的Country id;你应该有像country.getId();
-
ResultSet resultSet = sqlStatement.executeQuery("SELECT language FROM COUNTRY_LANGUAGE where id='countryIdVariable'");
ResultSet resultSet = sqlStatement.executeQuery(“SELECT language FROM COUNTRY_LANGUAGE where id ='countryIdVariable'”);
-
change your sql query to be parametized/prepared statement so you can prevent sql injection and also add variable easier instead of having to write variable within single quote in your sql string.
将您的sql查询更改为参数化/预准备语句,这样您就可以防止sql注入并且还可以更容易地添加变量,而不必在sql字符串中的单引号内写入变量。
#3
AS menthion by Asura, First of all change your sql query that returns language where contryId is 'something'
作为Asura的AS menthion,首先改变你的sql查询,返回contryId是'某事'的语言
then in while loop add each language to languages list object by using add() method like this :
然后在while循环中使用add()方法将每种语言添加到语言列表对象,如下所示:
while (resultSet.next()) {
String language = resultSet.getString("Languages");
// add to list
languages.add(language);
}
}