aspose file tools*
The moose likes Other Open Source Projects and the fly likes How to append data in CSV file using CSVWriter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to append data in CSV file using CSVWriter?" Watch "How to append data in CSV file using CSVWriter?" New topic
Author

How to append data in CSV file using CSVWriter?

Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 55

I want to append data of two tables in a resultset. I have tried the below code but not getting the desired output only the first resultset data i.e. the first table data


I am getting Count as 0 in all three places Guide me please.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

HappyS Singh wrote:...I am getting Count as 0 in all three places Guide me please.



The count is zero because you are getting a result set with no results from your SQL query.


Steve
Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 55
Despite of count 0, I get all records of second query in CSV file.
But I want both output..means 1st queries output appended with 2nd queries output.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

But... the getRow() method returns the current row number. Which will always be zero when you haven't read any rows from the ResultSet, as in the code posted.
Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 55
Okay.I got about getRow().
Now how to append the result in CSV file.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Paul Clapham wrote:But... the getRow() method returns the current row number. Which will always be zero when you haven't read any rows from the ResultSet, as in the code posted.


Yeah, I mis-read the method name (I thought is was getRows()) and was gonna edit my post. Stopped when I saw responses so it wouldn't be ninja-editing.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

If you want to append to a file, then you should use the FileWriter constructor with the parameter which specifies that. The constructor you chose always overwrites the data in the file, if it already exists.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Or just open the file before the for loop and close it afterwards, rather than opening and closing it every time round the loop.
Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 55
thanks alot Paul Clapham and Adrian Burkett.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Adrian Burkett wrote:Or just open the file before the for loop and close it afterwards, rather than opening and closing it every time round the loop.


Yes, I think that's a better fix than my suggestion.
Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 55
Now I am able to write it in file..but when the ouput is large ,I get
exception
Happy S Singh
Ranch Hand

Joined: Nov 14, 2011
Posts: 55
Resolved..Just moved ps = conn1.prepareStatement(results[i].toString()); outside the for loop
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to append data in CSV file using CSVWriter?