Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing or exporting data from database to a text file in jsp  RSS feed

 
khamhielle gabriel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

can anyone help me with regards to , on hoe am i able to write the value from the database into a text file.

as of now i did tried using fileoutput stream but , unforunat;y iam only getting the last row of tha database table..


does anyone know how to do it appropriately? in order for all the data to be written on the text file?


Thanks in advance and God Bless always ^^
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you must get a result set from the database. I'm guessing you loop through that result set and on each time through that looop, you open a new FileOutputStream, right? What you want to do is open the FileOutputStream before the loop, write to it in the loop, and close it after the loop.

If that's not it, then post some code and we'll see if we can spot the problem.
 
khamhielle gabriel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply Greg Charles,

well obviously i wasnt able to incorporate yet a loop just like what you have said,
and also ,im pretty new to java so sorry for my coding

hope you could help me handle it though.
you see with the codes i have now i was able to output only the last
database value in a text file.

but i need to take the whole content of the table.
any suggestions?examples are are also pretty welcome for my better understanding also.

super thank you ^^
GOD bless.



 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so this code isn't the body of a loop? In that case, then you're only running it once and you're actually getting just the first row of you results. Why can't you use a loop? Do you not know loop syntax, or is it just result sets that are the problem?

In any case, try here: http://download.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
 
khamhielle gabriel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh im sorry is it the while loop you are referring about?
yes indeed it is the body of that, and yes it did confused me a lot why isn't
outputting the who content of the database table since when i just try to display it on a page it was
doing well.

so i thought you are referring to another loop.


here is a a bigger part of my code anyway Thanks ^^




you can revise some if you feel like doing so. Thank you and God Bless.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you open and close the output file inside the loop, which is exactly what I guessed.
 
khamhielle gabriel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i revised some of my codes and i also put on some comment on the close command .

and it was now working fine. ^^

thank you. but for better understanding can you explain to me what happened in the process there?

Thanks again and God Bless Always
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean, why did you just get the one row of your database result in the file? Imagine that you're writing on paper and when you get to the end of a line, instead of going down to the next line, you go back to the beginning of the same line and write on top of what you wrote before. You end up with just one (really messy) line. Now further imagine that you have a magic pen, which erases everything you wrote previously when you start writing on a line. You end up with just the last line you wrote. That's what you were doing by opening and closing the file inside the loop body. Each time you open a file with a FileOutputStream, it clears out the previous contents of that file.
 
khamhielle gabriel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i understand. thank you very much for the knowledge mr.Greg Charles. ^^


God Bless Always.

Case Solved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!