gpt4 book ai didi

Java错误 'Never used locally'

转载 作者:行者123 更新时间:2023-12-02 04:41:38 25 4
gpt4 key购买 nike

在这个 java 项目中,我在显示从数据库中提取并存储在 ArrayList 中以在 readCountry 类中读取的语言时遇到问题。我的问题是为什么它不显示语言以及为什么它告诉我我的 readCountryLanguages 方法不是本地使用的?

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());
}
}

}

最佳答案

根据您发布的代码,readCountryLanguages() 方法永远不会在Country.java 内部调用。您的 IDE 会提醒您这一点,因为您通常不希望构建包含您不使用的代码的 .class 文件。

readCountryLanguages() 方法似乎也是您从数据库获取语言列表的地方。您可能想在某个时候调用此方法但忘记了。

关于Java错误 'Never used locally',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30115863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com