Read a text and formatt it

I want to read a text from a text area and store it in a DB. My problem is : how can I do to format my text before store it in db.
For example if I tape this text in my text area:

"The sun was shining on the sea,
Shining with all his might:
He did his very best to make
The billows smooth and bright-
And this was odd because it was
The middle of the night."

how read the carriage return and marked them with a special character?
And when I get my text from db to a txt file, how formatting it and have a text like over.  

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What kind of statement are You using? Is it Statement or PreparedStatement?
there are several other special characters rather than just carriage return. how do you want to escape them ?
simply use PreparedStatements, then escaping would be done automatically

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You should use PreparedStatement for efectiveness and greater possibilities:

//I assume here You have a table 'table', having one field of type varchar(100).
//Then the prepared statement is created, where the char '?' is a placeholder for a fields value in subsequent operations
PreparedStatement ps = connection.prepareStatement("INSERT INTO table VALUES(?)");
//now insert the value
ps.setString(1, textArea.getText());

For more information on how to use PreparedStatemet's see here:
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

why do you need to m,ark special characters, would appear you should be just saving the text as is.
String var = "this is the text you want to mark";
var = var.replaceAll("\n", "$$$"); // where $$$ is the special char
// then write to the DB

On reverse, just do it in reverse:

var = var.replaceAll("$$$", "\n");


Use chr(13)||chr(10) for a \n.

Then on select query, you do not need to do anything.
LepreRossaAuthor Commented:
but I have explained badly my problem.
I would replace carriage returns with a personal tag like :"\nt" when I read my text from a jsp and then I would to replace my special tag "\nt" with carriage return when diplay my text on jsp.
I have found this function that works on client side, I need something like this but on sever side:

function escapeVal(textarea,replaceWith){
textarea is reference to that object, replaceWith is string that will replace the encoded return
textarea.value = escape(textarea.value) encode textarea string's carriage returns

for(i=0; i<textarea.value.length; i++){
loop through string, replacing carriage return encoding with HTML break tag

if(textarea.value.indexOf("%0D%0A") > -1){
Windows encodes returns as \r\n hex
else if(textarea.value.indexOf("%0A") > -1){
Unix encodes returns as \n hex
else if(textarea.value.indexOf("%0D") > -1){
Macintosh encodes returns as \r hex


textarea.value=unescape(textarea.value) unescape all other encoded characters
This problem appears to have been resolved.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.