0

The java code should generate a csv file from the result of a select query.

Say example select * from employee;. The output of the query should be in csv file in the destination path mentioned in the code.

Please help.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
jyothi
  • 11
  • 2
  • 3
  • 1
    Lots of CSV API recommendations here: http://stackoverflow.com/questions/101100/csv-api-for-java – skaffman Feb 23 '10 at 13:41
  • Also http://stackoverflow.com/questions/200609/can-you-recommend-a-java-library-for-reading-and-possibly-writing-csv-files – defines Feb 23 '10 at 14:30

3 Answers3

4

Don't do it in Java. Any decent database already provides builtin facilities for this. It won't be any more efficient in Java than the DB does. Just consult its documentation for export facilities. As you didn't mention which one you're using, I'll just give a MySQL targeted example: LOAD DATA INFILE.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

I'm not sure what your trouble is. Generating CSV files is pretty much trivial in any language that supports file I/O. Parsing CSV files is where things get interesting.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • Generating CSV is not trivial. What do you do when one of the values you want to output contains a comma? e.g. "Hello, world!" Double quotes? e.g. 'He said: "Hello, world!"' Escaped double quotes? And so on.. – gb96 Apr 08 '13 at 06:33
0

Skaffman made a good comment... you can check out other posts in SO about CSV parsers. For example, check out OpenCSV.

Ascalonian
  • 14,409
  • 18
  • 71
  • 103