繁体   English   中英

如何从Country类中调用对象方法?

[英]How do I call an object method from my Country class?

我的目标是从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;
}



 } 

您的代码应如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM