Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes How can i show backend table data in CSV format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can i show backend table data in CSV format" Watch "How can i show backend table data in CSV format" New topic
Author

How can i show backend table data in CSV format

Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
Hi
Need help in coding a swing app such that, once if user clicks a button the he should get the respective table data in CSV format which he can save into his local machine.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

What have you got so far?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
I have a table in the database with batchId as the primary key. Now i have to design a screen with a textfield and a button. Once the user inputs the batcid in the textfield and if clicks the button then he should get the respective details in a CSV format
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Srinivasa Maddi:
I have a table in the database with batchId as the primary key. Now i have to design a screen with a textfield and a button. Once the user inputs the batcid in the textfield and if clicks the button then he should get the respective details in a CSV format


Ok.
Where are you stuck at? Getting the data from the DB or exporting it as CSV?
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
I can get it from DB but no idea how to export it to a CSV file
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Srinivasa Maddi:
I can get it from DB but no idea how to export it to a CSV file


CSV is a relatively simple file format.
You can write "This,is,a,test" in any text editor, save the file as test.csv and open it with excel, it will show you 4 columns populated with the above words. As the name suggests, it is a "comma separated value" file format.

When you make your DB query, you are getting back the ResultSet.
You can take the following approach.
Create a StringBuilder.
Iterate through the result set.
Pick out all values you want to store on the file and append them to the StringBuilder.
You need to append commas after each value for a row.
After each row you need to append the '\n' or line.separator.
Once you are done with all your ResultSet, just write the builder's string value to the file of your choice.
Check out the FileWriter class. It should suit your purpose.

Best of luck.
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
Thank you Manish. I will try to implement it
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

I suggest using a library such as opencsv. With this one you can write a String[] and the library will handle all the escaping et all.

The CSVWriter class even has a method for printing the column names given a ResultSet object. And if you're really lazy, how about writeAll(ResultSet, boolean) which does all the work for you?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Thanks Rob!
I wasn't aware of "opencsv".
It seems more easier than what I was suggesting.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Well in the end it will do just that, but somebody else has done the coding
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
Thank you all for your support.

Rob, i really liked the csvWriter but no idea how to use that in my code that i wrote as per Maneesh suggestion. See my code snippet below



Now my doubt is:
1. How to replace the above code to use csvWriter ?
2. What do i need to do if i want the above result in pdf format ?

In the above code i tried result.pdf instead of result.csv to get the output in pdf. Though its generating a pdf file but i cannot see its content. Popinng up a msg saying the file is either damager or acorbat doesnt support...(Sorry unable to attach the screenshot).



Thanks in advance
Maddi

[ November 06, 2008: Message edited by: Srinivasa Maddi ]
[ November 07, 2008: Message edited by: Srinivasa Maddi ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Srinivasa Maddi:
2. What do i need to do if i want the above result in pdf format ?


You cannot just name the file with .pdf extension. This is akin to renaming any file to .pdf.

You need something like IText to generate pdf files through java.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20


It's that easy.

About PDF, as Maneesh said it is a completely different format. In this case, you will have to do all the work yourself.
[ November 06, 2008: Message edited by: Rob Prime ]
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
Rob, could you please tell me what libraries do i need to import for using CSVWriter (i mean is there any thrid party library that i need to download here)?
[ November 07, 2008: Message edited by: Srinivasa Maddi ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
For creating PDFs you need the iText library. It has extensive documentation online, including many examples.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

And for CSVWriter you need opencsv. I've already posted a link to it earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i show backend table data in CSV format