如何将单个SELECT sql语句的EACH行结果转换为不同的输出文件? 例如,如果我有
SELECT * FROM myTable WHERE columnId < 45;
while (rs.next()) {
result of row1 goes to row1.txt;
result of row2 goes to row2.txt;
result of row3 goes to row3.txt;
result of row4 goes to row4.txt;
}
答案 0 :(得分:0)
我会这样做(不是生产质量代码,而是基本想法):
private String spoolRow(ResultSet currentRow)
{
// Append each attribute to a StringBuilder in a nicely formatted way
// and return.
}
private void myMethod(....)
{
...
...
FileOutputStream fos;
String fileName;
int counter = 1;
while (rs.next()) {
fileName = "row" + counter + ".txt";
fos = new FileOutputStream(fileName);
fos.write(spoolRow(rs));
fos.close();
counter++;
}
...
...
}
如果您还需要列标题,那么来自ResultSetMetaData
的{{1}}可用于获取列名,准备标题并在写入数据行之前将其写入每个文件。