タイトルはそれをすべて言います。これは私のResultSetコードです:ファイルに書き込むためにSQL結果を得るJava
public void writeToFile(int id, String name, String countryCode, String district, int population) throws IOException {
FileWriter fw = new FileWriter("C:\\Users\\horry\\Desktop\\results.txt");
fw.write(id + " " + name + " " + countryCode + " " + district + " " + population + "\n");
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
アイブ氏は、通常のフラッシュと密接な方法を試みたが、何かが欠けている:
try {
while (rs.next()) {
int ID = rs.getInt("ID");
String Name = rs.getString("Name");
String CountryCode = rs.getString("CountryCode");
String District = rs.getString("District");
int Population = rs.getInt("Population");
//Display values
try {
writeToFile(ID, Name, CountryCode, District, Population);
} catch (IOException e) {
e.printStackTrace();
}
}
これは私のWRITETOFILE方法です。ファイルライターがelsewgereと宣言されるべきですか?
例外なく以下のコードをご確認ください。 1行は書き込まれますが、不正なIDが付きます。あなたはもっと良い方法を知っていますか? –
デバッガ(またはロギング)を使用して、name、countryCodeなどの変数が設定されているかどうかを確認できますか(またはこれらのパラメータにハードコードされた値を渡すテストケースを記述しますか)。 – John