title: Writing table data in to multiple CSV files in java
body: <p>I have a table called employee having 10 000 records.</p>
  
  <p>I have to write these data into multiple CSV files having 2 000 records each by using multithreading Java.</p>
  
  <p>Sample code is below:</p>
  
  <pre><code>    public class PrintbyThreads {
  
      public static void main(String[] args) {
          ExecutorService executor = Executors.newFixedThreadPool(5);
          for (int i = 0; i &lt; 5; i++) {
              Runnable worker = new PrintFiles("" + i);
              executor.execute(worker);
            }
          executor.shutdown();
          while (!executor.isTerminated()) {
          }
          System.out.println("Finished all threads");
      }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class PrintFiles extends Thread implements Runnable {
      private String command;
  
      PrintFiles() {
          super("my extending thread");
          System.out.println("my thread created" + this);
          start();
      }
  
      public PrintFiles(String command) {
          this.command = command;
      }
  
      public void run() {
          System.out.println(Thread.currentThread().getName()
                  + " Start. Command = " + command);
          dataBaseExecution();
          System.out.println(Thread.currentThread().getName() + " End. Command ="
                  + command);
      }
  
      public synchronized void dataBaseExecution() {
          String tableName = "Employee";
          String filename = "D:/db2csv/";
          int recordsAtTime = 2000;
          try {
              Class.forName("oracle.jdbc.driver.OracleDriver");
              Connection con = DriverManager.getConnection(
                      "URL", "Uname", "Password");
              Statement stmt = con.createStatement();
              stmt.setFetchSize(recordsAtTime);
              ResultSet rs = stmt.executeQuery("select empid,empname,managerid from Employee");
              int columnCount = rs.getMetaData().getColumnCount();
              FileWriter fw = new FileWriter(filename + "" + tableName + ".csv");
              for (int i = 1; i &lt;= columnCount; i++) {
                  fw.append(rs.getMetaData().getColumnName(i));
                  fw.append(",");
  
              }
              fw.append(System.getProperty("line.separator"));
              while (rs.next()) {
                  for (int i = 1; i &lt;= columnCount; i++) {
                      if (rs.getObject(i) != null) {
                          String data = rs.getObject(i).toString();
                          fw.append(data);
                          fw.append(",");
                      } else {
                          String data = "null";
                          fw.append(data);
                          fw.append(",");
                      }
  
                  }
                  // new line entered after each row
                  fw.append(System.getProperty("line.separator"));
              }
  
              fw.flush();
              fw.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public String toString() {
          return this.command;
      }
  }
  </code></pre>
  
  <p>My code is giving single CSV file having all 10000 records.but I need 5 CSV files having 2000 records in each.  </p>
  
  <p>Thread1 has to process first 2000 records into employee1.csv</p>
  
  <p>Thread 2 has to process another 2000 records into employee2.csv</p>
  
  <p>,,,,,Etc.</p>
  
  <p>By using my code of threading, how can I achieve this requirement?</p>