-1

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.

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) {
            }
    }

}

JProgrammer
  • 13
  • 1
  • 1
  • 2

4 Answers4

3

Change this line:

for (String s : list) {

to

for (Object s : list) {
fajarkoe
  • 1,543
  • 10
  • 12
1

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.

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();
}
Dinal
  • 661
  • 4
  • 9
1

Don't use raw types! Change List data = new ArrayList(); to

List<String> data = new ArrayList<String>();

and change writeToFile(java.util.List list, String path) { to

writeToFile(java.util.List<String> list, String path) {
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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:

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.

If that doesn't work, make sure that your code is connecting to the database properly.

jluckin
  • 662
  • 4
  • 6