2015-05-09 3 views
0

Моя цель - вызвать метод объекта getId из моего класса Country, а также мне нужно вставить петлю в список моей страны перед моей командой sql readCountryLanguages.Как мне вызвать метод объекта из класса Country?

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

Я не понимаю, что вы пытаетесь спросить, вы можете уточнить? – Water

+0

Я пытаюсь вызвать метод объекта 'getId' в классе Country, а также мне нужно создать цикл над списком моей страны перед моей командой sql readCountryLanguages. – wisenhiemer

+0

для вызова getId() вам нужна ссылка на класс страны, вы можете использовать свой список, например, country.get (i) .getId(); но не уверен, что вы пытаетесь сделать здесь, хотите ли вы установить язык каждой страны в странах? – DoubleMa

ответ

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(); 
Смежные вопросы