Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export data from the database into .csv format programmatically?

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.

like image 368
Yatendra Avatar asked Feb 28 '23 18:02

Yatendra


2 Answers

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

like image 175
hmm Avatar answered Mar 05 '23 17:03

hmm


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.

like image 37
easeout Avatar answered Mar 05 '23 17:03

easeout