title: Convert Resultset to CSV file using Java
body: <p>Hi I am trying to convert oracle jdbc resultset to csv file. Below is the code used. Issue occures when there is value like below in the field. It deforms the output csv and all this come in separate line rather than in one field.</p>
  
  <p>Value in Field comes in csv as   </p>
  
  <blockquote>
    <p>[&lt;333message:Runtime error in script' ProcessItem: ' Type: 'ITEM'" 1:0).Internal Script     error: java.lang.NullPointerException<br>
        Script (line 1):<br>
        <B>  setHours = 0 ;</B><br>
        if(ts.instanceId == null)<br>
        "   3 :     ts.instanceId = 0 ;"<br>
        Step >] </p>
  </blockquote>
  
  <pre><code>int ncols = result.getMetaData().getColumnCount();  
  
              System.out.println("ColumnCout"+ncols);  
              FileOutputStream fos=new FileOutputStream(new File("C:\\test.csv"),false);  
              Writer out = new OutputStreamWriter(new BufferedOutputStream(fos),"UTF_8");      
  
              for (int j=1; j&lt;(ncols+1); j++) {     
              out.append(result.getMetaData().getColumnName (j));       
              if (j&lt;ncols) out.append(","); else out.append("\r\n");      
              }   
              int m =1;    
  
              while (result.next()) {   
  
              for (int k=1; k&lt;(ncols+1); k++) {   
  
              out.append(result.getString(k));    
  
              if (k&lt;ncols) out.append(","); else out.append("\r\n");    
              }   
              //System.out.println("No of rows"+m);   
              m++;   
              }  
  </code></pre>