• 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

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

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the problem in accessing and reading the DB, or in writing the file? What do you have so far?
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 81
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the issue you are getting when writing to the text file ? any exceptions ?

~Santhosh
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 81
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sumantha panda

can you please send the full code for exporting data from database to text file.

Kindly help me in this .

Thanking you
Krishna
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Since it is a long time since this thread was opened, you may not get a reply. We do not usually hand out ready‑made code. Are you paying for code, or would you like to show us what you have tried?
 
reply
    Bookmark Topic Watch Topic
  • New Topic