gpt4 book ai didi

java - 如何为每个 sql 记录创建单独的输出文件?

转载 作者:行者123 更新时间:2023-11-29 07:48:15 24 4
gpt4 key购买 nike

public void selectdata(Connection conn, String database){

String table = "Mo"; // using Mo table,
String comandoSql = "SELECT * FROM " + database + "."+"dbo"+ "."+ table;
try {
stmt = conn.createStatement();

rs = stmt.executeQuery(comandoSql);
System.out.println("Command sql: ");
while (rs.next()) {
String DocumentName = rs.getString(2);
doc1 = DocumentName;
try {

File file = new File("///.txt"); // path to output folder.
FileWriter fileWritter = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fileWritter);
out.write(doc1);
out.newLine();
out.close();

} catch (IOException e1) {
System.out.println("Could not write in the file");
}
}
} catch (SQLException e) {
e.printStackTrace();
}

这会生成输出文件,但包含一个文件中的所有记录,我的问题是如何为每个记录生成单独的文件,不想进行后期处理以生成单独的文件?有什么帮助吗?扫描仪不喜欢,因为不知道一条记录中有多少行(它是一种存储到数据库表中的多个文档!!!)

最佳答案

只需为每条记录创建一个新文件:

 int counter = 0;

while (rs.next()) {
String DocumentName = rs.getString(2);
doc1 = DocumentName;
try {

File file = new File("///" + counter + ".txt"); // path to output folder.
FileWriter fileWritter = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fileWritter);
out.write(doc1);
out.newLine();
out.close();
counter++;

} catch (IOException e1) {
System.out.println("Could not write in the file");
}
}

只需创建一个计数器变量或其他变量来区分每个文件。将计数器附加到文件名,以便为每个记录创建一个新文件...

关于java - 如何为每个 sql 记录创建单独的输出文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27151931/

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