Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to append data in CSV file using CSVWriter?

 
Happy S Singh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Happy S Singh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.I got about getRow().
Now how to append the result in CSV file.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot Paul Clapham and Adrian Burkett.
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I am able to write it in file..but when the ouput is large ,I get
exception
 
Happy S Singh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resolved..Just moved ps = conn1.prepareStatement(results[i].toString()); outside the for loop
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic