aspose file tools*
The moose likes I/O and Streams and the fly likes How to write contents of oracle table into csv file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to write contents of oracle table into csv file? " Watch "How to write contents of oracle table into csv file? " New topic
Author

How to write contents of oracle table into csv file?

prasanna ganesh
Greenhorn

Joined: Jul 04, 2011
Posts: 9
Hi,
I'm trying to write content of oracle table into a csv file for one of my task.
First i tried to add the column name into csv file followed by writing the column value into the csv file.
But when i try to add my column name to csv file its showing only my last column name value cos its overwriting existing column name value each time.

Can some one help me to solve this issue?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18860
    
    8

I'm confused about what a "column name value" is. I'm also confused about why your code doesn't actually write the contents of the table to the CSV file... is that what you are asking about?

Perhaps it would help if you showed an example of your output and explained why it was wrong?
prasanna ganesh
Greenhorn

Joined: Jul 04, 2011
Posts: 9
Hi,
Thanks for your reply..

You are right so far i didn't write code to add the table value into my csv file.

Before that i want to add my column name into csv file.

My table value should like below, where first line is my column name and followed below its value.

SKU_CODE SKU_UPC LIST_PRICE SALE_PRICE
XXXXXX YYYYY 36 0
ZZZZZ AAAAA 45 0


Right now with my code i can see only the last column name "SALE_PRICE" in my test.csv file.

PFA for your reference.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18860
    
    8

I don't know who "PFA" is.

Anyway you should do some debugging to see what your code is doing. Wait... I see you already are. But you haven't told us the results of that. Keeping your data secret isn't helpful. Tell us what you already found out.
prasanna ganesh
Greenhorn

Joined: Jul 04, 2011
Posts: 9
Sorry Paul,
I attached my output file with my previous reply. But it was somehow missed... Once again sorry because it was not accepting csv and xls file as attachment.

I hava four columns(sku_code,sku_upc,list_price,sale_price) in my table.

SKU_CODE SKU_UPC LIST_PRICE SALE_PRICE
400567461373 883834147291 36 0
400568822197 400568822197 45 0
400430619481 85447978723 11 0

But with my current code it was only writing sale_price column in my test.csv file because it was overwriting my previous coloumn name values.

SALE_PRICE


I think now you can able to understand my issues.

I'm anticipating a solution from you and let me know where i was wrong with my code.

Thanks again.




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18860
    
    8

Paul Clapham wrote:Anyway you should do some debugging to see what your code is doing. Wait... I see you already are. But you haven't told us the results of that. Keeping your data secret isn't helpful. Tell us what you already found out.
prasanna ganesh
Greenhorn

Joined: Jul 04, 2011
Posts: 9
Hi Paul,

You are not clear with my previous reply? I pasted my output of my actual table values and output which i got.

Let me know if you are not clear.

Actual outuput:

SKU_CODE SKU_UPC LIST_PRICE SALE_PRICE
400567461373 883834147291 36 0
400568822197 400568822197 45 0
400430619481 85447978723 11 0

My output:

SALE_PRICE
prasanna ganesh
Greenhorn

Joined: Jul 04, 2011
Posts: 9
Hi Paul,
Finally i got answer for my question. As you said i did some more debug with my code and modified it slightly to get the expected result for my task.

Thanks for your guidance.

Here is my code

Vivek Kr Singh
Ranch Hand

Joined: Oct 12, 2007
Posts: 56
There is an API available for this exact functionality. Please see http://opencsv.sourceforge.net/#sql-integration


SCJP 1.4
prasanna ganesh
Greenhorn

Joined: Jul 04, 2011
Posts: 9
Hi Vivek,

Thanks for this.. If i would have got your reply some time earlier itself, it was so useful for my task..

Anyhow i come to know one new api for csv stuff.. Thanks once again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

Your code has two flaws:
1) it's printing spaces between the first few columns.
2) it won't handle commas inside values well.

I was also going to recommend opencsv; it's designed to handle all these cases, and it has an easy way to write a ResultSet to the CSVWriter.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write contents of oracle table into csv file?