2015-05-08 35 views
0

在這個java項目中,我遇到了顯示從數據庫中提取並存儲在ArrayList中以讀取我的readCountry類的語言的問題。我的問題爲什麼不會顯示語言,爲什麼它告訴我我的readCountryLanguages方法不在本地使用?Java錯誤'從未本地使用'

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

}

+0

當前代碼的作用是什麼,以及您認爲問題出在哪裏? – Cristik

+0

@wisenhiemer請標記正確的答案,如果它解決了您的問題。 –

回答

2

根據您發佈的代碼,該readCountryLanguages()方法不會被調用內部Country.java。您的IDE正在提醒您這一點,因爲您通常不希望構建包含您不使用的代碼的文件.class

readCountryLanguages()方法似乎也是您從數據庫中獲取語言列表的地方。你可能想在某個時候調用這個方法,但忘記了。

+1

可能值得補充的是,通常這是一個警告,而不是一個錯誤(除非你有非常嚴格的編譯器選項設置),所以即使使用這個消息你也可以正常運行你的代碼。 –

+0

你是不是指ReadCountryDB?我的readCountryLanguages()方法位於ReadCountryDB類中。 – wisenhiemer

相關問題