![](/img/trans.png)
[英]Do I have to create a separate ActionListener for each component I create?
[英]How do I create separate output file for each sql record?
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");
}
}
只需創建一個計數器變量或用於區分每個文件的內容即可。 將計數器添加到文件名,以便為每個記錄創建一個新文件...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.