0

如何将单个 SELECT sql 语句的每行结果假脱机到不同的输出文件?例如,如果我有

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;
}
4

1 回答 1

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++;
    }
    ...
    ...
}

如果您还需要列标题,则可以使用ResultSetMetaDatafromresultSet.getMetaData()获取列名,准备标题并将其写入每个文件,然后再写入数据行。

于 2013-10-14T14:21:24.417 回答