• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

csv output using spring-jdbctemplate during fetching multiple rows

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using spring jdbctemplate to interact with Oracle database. For fetching multiple rows for a given condition, I want to store the result in a csv file having Column names as the header.

I tried few ways -



In customizedrowmapper, I am fetching the rows from result set and setting the fields in Bean class. I could not find any easy way to convert from bean to csv file. As I dont have access to bean class code.

2nd way I found was to writer ResultSet directly to csv file like below using opencsv library.



This is working okay, but it is skipping one row while writing to csv. I tried few queries. Manually, I can see 4 rows in the result, but in the CSV, it stores only three rows. Anyone faced a similar issue?

Or any other way we can achieve the same, without manually getting each record from ResultSet and making it comma-separated string and storing in file.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you check if the first row of the ResultSet is not in the output? Maybe you called rs.next() once and the CSVWriter is calling rs.next() again thus skipping the first row?
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic