• 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

Database String in jsp

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have saved the following String in Database and want to print it in a Text Area on my jsp.

The String is
I am here \n You are there \n I am not going any where.

If I hardcode the String, I get the three String each in new line.

Whereas the String from database prints the above string as is. I want to print the string in new line inside a text area.

The Stringtokenizer also does not work for this one.

Please Let me know, a way to acheive this.

Any help is greatly appreciated.

Thanks in advance
-Priya
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya,
Can you show the code you are using to create the textbox with the new lines? I think it is a
vs \n thing, but it's hard to tell without seeing the code.
 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya, you can do it this way. Consider a sample code and the string you are retrieving from database is in first column:



Actually I executed this code. It is working. I am getting a new line every time. If this doesn't work try with the first one.



I created a table named abc which contains abcd\nefgh\nijkl and I tried to retrieve it. It came as I expected. The sentance was splitted into three different lines.



You can use any of the methods. I think there is a mistake in <textarea> tag. Once check it. I think the first one is a bad practice. You better check your tag and follow the third one.

Have a nice day.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
priya!


html page does not know about "\n" character of java.

"\n" in java = <br> of html. try <br> tag. you will get what you need!

or


out.print("abcd <br> defg <br> hijk");

will return,
abcd
defg
hijk.


 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yuvaraj yuvan wrote:priya!


html page does not know about "\n" character of java.

"\n" in java = <br> of html. try <br> tag. you will get what you need!

or


out.print("abcd <br> defg <br> hijk");

will return,
abcd
defg
hijk.



no it wont happen.



This will yield an output like this

abcd<br></br>efgh in the text area.
 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't close the <br> tag pa. its just <br>! thats it.
 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<html>
<body>
<textarea> abcd <br> defg <br> </textarea>
</body>
</html>


abcd
defg
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yuvaraj yuvan wrote:<html>
<body>
<textarea> abcd <br> defg <br> </textarea>
</body>
</html>


abcd
defg



Please once check the code. Execute it once. You will get abcd <br> defg <br> in the text area.

 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry! you are correct.


you may try this.
this should work.

<textarea> Test& #13; Newline gadsg</textarea>


thanks
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yuvaraj yuvan wrote:i am sorry! you are correct.


you may try this.
this should work.

<textarea> Test& #13; Newline gadsg</textarea>


thanks



Its ok.
 
priya Anand
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for your replies.

But I am still not getting the thing right.

Anusha, how did you store your string in Database.

I will explain my Problem once again.

String strText = "Line one\nLine two\nLine three"

System.out.println("And the Original String is : "+strText);

In the logs I see this as :
Line one(box)Line two(box)Line three

I have the same string in the database saved it as varchar2.

I retrieve it and print it.

System.out.println("Index is at : "+rs.getString("VALUE"))

In the logs I see this as :
Line one\nLine two\nLine three

I tried to get the index of \n and I get back a -1.

The String retrieved from the Database has a different behaviour than the one I have hard coded.

Please let me know how can I retrieve this String as a normal String from the Database.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya, it is not possible to give suggestions further until you paste a sample code. Please paste your code.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Using textarea does not give line-break , which you may try with a simple HTML and see.
2. Using \n also does not give line-break.
3. You need to use tag for this purpose.
4. Using table option does provide the line-break.
5. The column value used, for the database is, 'I am here \n You are there \n I am not going any where. '
6. The jsp code is as follows.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic