title: JAVA Code to print SQL Server to CSV returns with incomplete data
body: <p>I have a JAVA code to out put SQL Server Table to CSV. The printing is stopping in the middle and I am getting only partial data as CSV output. Below my code.</p>
  
  <pre><code>public class SQLServerConnection {
  
      public static void main(String[] args) throws IOException, SQLException {
          // TODO Auto-generated method stub
  
          Connection conn = null;
          SQLConnection cnn = new SQLConnection();
          FileWriter fw = new FileWriter(cnn.fileName);
  
          try {conn = DriverManager.getConnection(cnn.dbURL);
  
          if (conn != null) {
              DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
          }
          } catch (SQLException ex) {
              ex.printStackTrace();
          } 
  
          String sql = String.format(cnn.Query);
          assert conn != null;
  
          PreparedStatement preStatement;
          preStatement = conn.prepareStatement(sql);
          ResultSet result = preStatement.executeQuery();
          ResultSetMetaData rmsd = result.getMetaData();
          int Columncount = rmsd.getColumnCount();
  
        //Get the column name and print the column name
          for (int iterator=1; iterator&lt;= Columncount; iterator++) {
              fw.append(rmsd.getColumnName(iterator)+",");
          }
          fw.append('\n'); 
  
          while(result.next()){
              try {
                  for (int jterator=1; jterator&lt;=Columncount; jterator++){
                      fw.append(result.getString(jterator));
                      fw.append(',');
                  }
                  fw.append('\n');
               } catch (SQLException e)
              {
                  e.printStackTrace();
              }}
          conn.close();
      }}
  </code></pre>
  
  <p>Class for DB and Excel Parameters</p>
  
  <pre><code>public class SQLConnection {
      public String ServerName = "Server1";
      public String DBName = "DB1";
      public String FileLoc = "Location of the file";
      public String dbURL = "jdbc:sqlserver://"+ServerName+";databaseName="+DBName+";integratedSecurity=true";
      public String Query = "SELECT * from [scdHSBCHK]";
      public String fileName = (FileLoc + DBName +"_QueryResult.csv"); 
  }
  </code></pre>
  
  <p>The actual db returns 57 records but the csv returns only 29. I tried with different db names as well same issue though. When I output the results in the program window, the data is displaying correctly.</p>