views:

225

answers:

3

I have stored some data in MySQL database through a java program. Now I want to export that data from the same program in .csv file.

I know One method of doing this is to get all the fields of one row in variables and then store them in a file by seperating them with a comma(,) and by repeating the same for every row in the database.

But I want to know that Can I export the same data through any other way through java in .csv format.

+2  A: 

You can Google for a third party CSV library, do the Java querying yourself with sqljdbc.jar or whatever is appropriate to your database, and feed the data to the CSV library.

You might even find a third-party library that can give you a CSV from a SQL query and a JDBC connection.

But if you're going to programmatically implement CSV, please implement it completely, including escaping spaces, quotes, etc. Wikipedia has details.

Kevin Conner
+1 for wikipedia link.
Yatendra Goel
+2  A: 

I used this once it worked for me. http://opencsv.sourceforge.net/

Honestly, unless you want to do something fancy, what you are doing should work

hmm
+2  A: 

If you are using MySQL take a look at mysqldump

Yada