?
Solved

Read a text and formatt it

Posted on 2006-04-09
12
Medium Priority
?
165 Views
Last Modified: 2010-03-31
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.  

Thnks!
0
Comment
Question by:LepreRossa
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16412213
What kind of statement are You using? Is it Statement or PreparedStatement?
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 100 total points
ID: 16412283
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
0
 
LVL 14

Assisted Solution

by:StillUnAware
StillUnAware earned 100 total points
ID: 16412297
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:
http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 16413165
why do you need to m,ark special characters, would appear you should be just saving the text as is.
0
 
LVL 8

Assisted Solution

by:colr__
colr__ earned 100 total points
ID: 16415345
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");

colr__
0
 
LVL 5

Assisted Solution

by:mrigank
mrigank earned 100 total points
ID: 16419390
http://www.experts-exchange.com/Databases/Oracle/Q_21277128.html

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

Then on select query, you do not need to do anything.
0
 

Author Comment

by:LepreRossa
ID: 16425425
Sorry,
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
textarea.value=textarea.value.replace("%0D%0A",replaceWith)
}
else if(textarea.value.indexOf("%0A") > -1){
Unix encodes returns as \n hex
textarea.value=textarea.value.replace("%0A",replaceWith)
}
else if(textarea.value.indexOf("%0D") > -1){
Macintosh encodes returns as \r hex
textarea.value=textarea.value.replace("%0D",replaceWith)
}

}

textarea.value=unescape(textarea.value) unescape all other encoded characters
}
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 16425504
0
 
LVL 8

Expert Comment

by:colr__
ID: 16646804
This problem appears to have been resolved.

colr__
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question