标题说明了一切。这是我的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