Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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?
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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:

 
Santhosh ayiappan
Ranch Hand
Posts: 80
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the issue you are getting when writing to the text file ? any exceptions ?

~Santhosh
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 80
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic