Convert Resultset to CSV file using Java

Devesh

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.

Value in Field comes in csv as

[<333message:Runtime error in script' ProcessItem: ' Type: 'ITEM'" 1:0).Internal Script error: java.lang.NullPointerException
Script (line 1):
setHours = 0 ;
if(ts.instanceId == null)
" 3 : ts.instanceId = 0 ;"
Step >]

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<(ncols+1); j++) {     
            out.append(result.getMetaData().getColumnName (j));       
            if (j<ncols) out.append(","); else out.append("\r\n");      
            }   
            int m =1;    

            while (result.next()) {   

            for (int k=1; k<(ncols+1); k++) {   

            out.append(result.getString(k));    

            if (k<ncols) out.append(","); else out.append("\r\n");    
            }   
            //System.out.println("No of rows"+m);   
            m++;   
            }  
Ravi K Thapliyal

Get the value for the column that could have new lines as

String multiLine = null;
if (k == <col_index> && (mutiLine = rs.getString(k)) != null)
    out.append(multiLine.replaceAll("\\n", ""));
else
    out.append(result.getString(k));

You could filter all the columns as well but then would incur some performance hit.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert an XML file to CSV file using java

From Dev

Convert an XML file to CSV file using java (with multiple element values)

From Dev

Convert tab file data to csv or excel format using java

From Dev

Write a sql ResultSet to a csv file

From Dev

Convert JSON file to a CSV file using R

From Dev

Convert .prn file to csv file format in java

From Dev

How to convert a text file to CSV using Bash

From Dev

How to convert a CSV file using a PHP script

From Dev

Unable to convert JSON file to CSV using Python

From Dev

convert soap log file to csv using powershell

From Dev

Using PHP how to convert csv file

From Dev

How to convert XML file to CSV using Perl?

From Dev

convert a text file to csv using shell script

From Dev

How to convert a CSV file using a PHP script

From Dev

How to convert a text file to CSV using Bash

From Dev

Unable to convert JSON file to CSV using Python

From Dev

Convert .csv file to .ods using terminal

From Dev

How to convert exponents in a csv file from Java

From Dev

JAVA convert string CSV file into double[] array

From Dev

Create CSV file using java

From Dev

How to export csv file of large resultset using cypher in Neo4j in the browser?

From Dev

How to convert following CSV to Hashmap using JAVA

From Dev

C# convert csv to xls (using existing csv file)

From Dev

Convert .txt file into .cvs file using java?

From Dev

Conversion of CSV file to LDIF file using java

From Dev

Converting a CSV file to an XML file using Java

From Dev

How to use Java ResultSet to convert the results to a String

From Dev

directly convert CSV file to JSON file using the Jackson library

From Dev

how to convert csv file to json using jquery and download that json file

Related Related

  1. 1

    Convert an XML file to CSV file using java

  2. 2

    Convert an XML file to CSV file using java (with multiple element values)

  3. 3

    Convert tab file data to csv or excel format using java

  4. 4

    Write a sql ResultSet to a csv file

  5. 5

    Convert JSON file to a CSV file using R

  6. 6

    Convert .prn file to csv file format in java

  7. 7

    How to convert a text file to CSV using Bash

  8. 8

    How to convert a CSV file using a PHP script

  9. 9

    Unable to convert JSON file to CSV using Python

  10. 10

    convert soap log file to csv using powershell

  11. 11

    Using PHP how to convert csv file

  12. 12

    How to convert XML file to CSV using Perl?

  13. 13

    convert a text file to csv using shell script

  14. 14

    How to convert a CSV file using a PHP script

  15. 15

    How to convert a text file to CSV using Bash

  16. 16

    Unable to convert JSON file to CSV using Python

  17. 17

    Convert .csv file to .ods using terminal

  18. 18

    How to convert exponents in a csv file from Java

  19. 19

    JAVA convert string CSV file into double[] array

  20. 20

    Create CSV file using java

  21. 21

    How to export csv file of large resultset using cypher in Neo4j in the browser?

  22. 22

    How to convert following CSV to Hashmap using JAVA

  23. 23

    C# convert csv to xls (using existing csv file)

  24. 24

    Convert .txt file into .cvs file using java?

  25. 25

    Conversion of CSV file to LDIF file using java

  26. 26

    Converting a CSV file to an XML file using Java

  27. 27

    How to use Java ResultSet to convert the results to a String

  28. 28

    directly convert CSV file to JSON file using the Jackson library

  29. 29

    how to convert csv file to json using jquery and download that json file

HotTag

Archive