使用java将记录从数据库写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24142608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write records into text file from database using java
提问by JProgrammer
I want to store database records to text file. for that I used the ResultSet, I have selected the whole table using sql select query and store the data in the ArrayList. But this code doesn't work properly. my database name is test and table is employee. what should I do? this is what I have tried.
我想将数据库记录存储到文本文件。为此,我使用了 ResultSet,我使用 sql select 查询选择了整个表并将数据存储在 ArrayList 中。但是这段代码不能正常工作。我的数据库名称是 test,表是员工。我该怎么办?这是我尝试过的。
import java.io.*;
import java.sql.*;
import java.util.*;
public class ResultSetWriteInToTextFile {
public static void main(String[] args) {
List data = new ArrayList();
try {
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from employee");
while (rs.next()) {
String id = rs.getString("emp_id");
String name = rs.getString("emp_name");
String address = rs.getString("emp_address");
String contactNo = rs.getString("contactNo");
data.add(id + " " + name + " " + address + " " + contactNo);
}
writeToFile(data, "Employee.txt");
rs.close();
st.close();
} catch (Exception e) {
System.out.println(e);
}
}
private static void writeToFile(java.util.List list, String path) {
BufferedWriter out = null;
try {
File file = new File(path);
out = new BufferedWriter(new FileWriter(file, true));
for (String s : list) {
out.write(s);
out.newLine();
}
out.close();
} catch (IOException e) {
}
}
}
}
采纳答案by fajarkoe
Change this line:
改变这一行:
for (String s : list) {
to
到
for (Object s : list) {
回答by Dinal
You cannot use for (String s : data) {}
. You have no where mentioned that data is a List of Strings. Declare it as List<String> data = new ArrayList<String>();
. Hope you are using Java 1.5 or higher.
您不能使用 for (String s : data) {}
。您没有提到数据是字符串列表。将其声明为List<String> data = new ArrayList<String>();
. 希望您使用的是 Java 1.5 或更高版本。
Also you need to give an absolute file path.
您还需要提供绝对文件路径。
String path = "D:/Sample/Employee.txt" //whatever path you are using
File file = new File(path);
File parent_directory = file.getParentFile();
if (parent_directory != null) {
parent_directory.mkdirs();
}
回答by jluckin
First make sure that your file path is correct, and that all of the directories exist.
首先确保您的文件路径正确,并且所有目录都存在。
Also, you have a try-catch statement in writeToFile
. That method is called inside another try-catch statement. In the function declaration for writeToFile
, try something like this:
此外,您在writeToFile
. 该方法在另一个 try-catch 语句中被调用。在 for 的函数声明中writeToFile
,尝试如下操作:
private static void writeToFile(java.util.List list, String path) throws IOException`
You can then remove the try-catch statement in writeToFile
, as if that exception is thrown, it is handled by the try-catch in the main method.
然后,您可以删除 中的 try-catch 语句writeToFile
,就像抛出该异常一样,它由 main 方法中的 try-catch 处理。
If that doesn't work, make sure that your code is connecting to the database properly.
如果这不起作用,请确保您的代码正确连接到数据库。
回答by Elliott Frisch
Don't use raw types! Change List data = new ArrayList();
to
不要使用原始类型!更改List data = new ArrayList();
为
List<String> data = new ArrayList<String>();
and change writeToFile(java.util.List list, String path) {
to
并更改writeToFile(java.util.List list, String path) {
为
writeToFile(java.util.List<String> list, String path) {