標題說明了一切。這是我的ResultSet代碼:無法獲得SQL結果寫入文件Java
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方法:
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();
}
}
我用盡通常的沖洗和close方法,但缺少的東西。應該向其他人宣佈編寫文件嗎?
沒有例外的代碼。一行被寫入,但ID不正確。你知道更好的方法嗎? –
您是否可以使用調試器(或日誌記錄)來查看是否正在填充名稱,countryCode等變量(或者編寫一個測試用例傳入這些參數的硬編碼值)? – John