This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes I/O and Streams and the fly likes In Java program how to export data from database to text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "In Java program how to export data from database to text file " Watch "In Java program how to export data from database to text file " New topic
Author

In Java program how to export data from database to text file

sumanta panda
Ranch Hand

Joined: Jun 23, 2008
Posts: 224
Dear All,

i'm a beginner to java.

just now i had tried to read and write files using java...

and then i had tried to connect a database using jdbc...

now i want to export the data's from a database into a text file using java.
Can any one please give me an idea for doing this process or give the pointer of the document which would help me to resolve this issue.

Thanks and Regards..
Sumanta Panda
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41592
    
  55
Is the problem in accessing and reading the DB, or in writing the file? What do you have so far?


Ping & DNS - my free Android networking tools app
sumanta panda
Ranch Hand

Joined: Jun 23, 2008
Posts: 224
Dear Ulf Dittmer,

I am able to accessing and reading the DB.
I want how to export the same data in the text file.

Please suggest.

Thanks and Regards,
Sumanta Panda
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41592
    
  55
Are you familiar with the classes in the java.io package, particularly File, FileWriter and BufferedWriter? Those can be used to write to files. If that's not what you're looking for, then tell us in more detail what you're struggling with.
sumanta panda
Ranch Hand

Joined: Jun 23, 2008
Posts: 224
Dear Ulf Dittmer,

Can any one please pointer me any url which will export data from database to text file.


Thanks for the understanding.

Regards,
Sumanta Panda
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41592
    
  55
What are you prepared to do yourself in order to accomplish this? Writing to a file will involve the classes I mentioned; have you checked them out yet?
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
As i got this topic, i am posting my question in the same topic.

I am able to retrieve the records from the database but i am not able to write it to the text file, so what could be the reason.
I am posting my code as follows:

Santhosh ayiappan
Ranch Hand

Joined: Jan 30, 2007
Posts: 80

What is the issue you are getting when writing to the text file ? any exceptions ?

~Santhosh
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Dinesh Remje wrote:

This way of creating a FileWriter overwrites the file each time. Check out the other constructors of FileWriter. Or, preferably, open the FileWriter before the loop, then close it afterwards. A try-finally block would be best for that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
@santosh: I am not getting any exceptions. I am able to write on the file, but the only thing is that instead of the particular ID it prints some symbol in the file.
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
@Rob Spoor: Ok i got that why i was able to write only single character on the file. But can you tell me why i am not able to write the value of "i" on the file. Instead it is writing some symbol on the file which is like this "ኸ".
Santhosh ayiappan
Ranch Hand

Joined: Jan 30, 2007
Posts: 80

write(int) is used to write a single char. Why do you want to convert the string to an Integer and then write to the file, Why cant you directly write the string to the file. ?

Check ou this . Writer Docs

~Santhosh
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
@santosh: thanks for the reply, i was able to do it, but i did it in the other way round i.e


But now my problem is each and every time it is creating the file instance and hence it is retaining only the last record from the database, so what could be done in order to retain all the 10 ID's.

As mentioned by Rob Spoor i tried it by creating the file out of the while loop but it doesn't help.



Dinesh
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
Thank you guys i was able to do it with the help of string buffer and then writing it all at once at last.


Thanks
Dinesh
 
Don't get me started about those stupid light bulbs.
 
subject: In Java program how to export data from database to text file