Convert Resultset to CSV file using Java

后端 未结 2 1551
独厮守ぢ
独厮守ぢ 2021-01-06 21:26

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

相关标签:
2条回答
  • 2021-01-06 21:34

    Are you using "java.sql.ResultSet" class? If yes, see the library in this link http://opencsv.sourceforge.net/

    See an example:

    CSVWriter csvWriter = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
    java.sql.ResultSet myResultSet = .... ;
    csvWriter.writeAll(myResultSet, includeHeaders);
    
    0 讨论(0)
  • 2021-01-06 21:42

    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.

    0 讨论(0)
提交回复
热议问题