title: Write records into text file from database using java
body: <p>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.</p>
  
  <pre><code>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) {
              }
      }
  </code></pre>
  
  <p>}</p>