views:

137

answers:

3

I want to put all the data in the resultset into a text file in the same order. Is there any method to get data in all the rows at once and write to a file or have to write it row by row?

+2  A: 

Why go through Java to do this? Many DBMS provide this feature out of the box.

MySQL example:

select
   your_first_field,
   your_second_field
from
   your_favorite_table
into
   outfile
      '/path/to/favorite/file.csv'
   fields terminated by ','
   enclosed by '"'
   lines terminated by '\n'
Björn
+1  A: 

ResultSet is not how you think it is. Its just a reference to the actual resultset in the database. You cannot convert it in one shot. You will have to iterate it row by row. Whenever a select query is fired, the result that is produced is held in the database cache for which jdbc allocates a resultset reference to make life easier to access data.

So, the answer to your question is, Yes you need to iterate row by row and probably you can use a CSV file to store your values.

Bragboy
A: 

1) Answer to this question can be handled in the following way

What ever the resultset you have got, iterate through the result set and when you are storing the data into the variables, send it to the csv files through the File I/O Package

2) if you have the database, from there directly export the data to a csv files.

harigm