gpt4 book ai didi

Java 对 ResultSet 的迭代过早停止

转载 作者:行者123 更新时间:2023-12-01 14:02:02 27 4
gpt4 key购买 nike

我正在尝试以 .csv 格式编写整个 sql 表。该表有 6500 行长,但文件中只写入了 600 行左右。每次运行该方法时,写入的行数都会有所不同

Connection con = Panel.getConnection();
String query = "SELECT * from group3.merged_person";

PreparedStatement statement = con.prepareStatement(query);

ResultSet srs = statement.executeQuery();

int totalColumns = srs.getMetaData().getColumnCount();
FileWriter writer = new FileWriter("merged_person.csv");

for(int i = 1; i <= totalColumns; i++) {
writer.write(srs.getMetaData().getColumnLabel(i) + ",");
}

writer.write("\n");
int lines = 0;

while(srs.next()) {
lines++;

for(int j = 1; j <= totalColumns; j++) {
writer.write(srs.getString(j) + ",");
}

writer.write("\n");
}

System.out.println("lines written " + lines);
writer.close();

最佳答案

您可以考虑添加:

writer.flush();

之前

writer.close();

这应该强制将所有缓冲内容写入目标文件。

关于Java 对 ResultSet 的迭代过早停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com