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();
}
这会生成输出文件但包含一个文件中的所有记录,我的问题是如何为每个记录生成单独的文件,不想做后期处理以生成单独的文件?任何帮助??????不喜欢扫描仪,因为不知道一条记录中有多少行(这是一种存储在数据库表中的多种文件!!!!)
答案 0 :(得分:2)
只需为每条记录创建一个新文件:
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");
}
}
只需创建一个计数器变量或其他东西来区分每个文件。将计数器附加到文件名,以便为每条记录创建一个新文件......