I have a simple query regarding ResultSet.getString()
method in java for JDBC.
Suppose the value in the Database column is having a \
which is javas escape character e.g. \n
or \t
etc.
When i retrieve the value as getString()
i see one more escape character is getting added and the actual meaning of this \n
is now a string literal only.
So i had to unescape java and then use it properly.
String s= rs.getString("col_name");
When s
contains `\n':
System.out.println(s)
output:
\n
After unescaping java using apache common StringEscapeUtils
output:
System.out.println("hi"+s+"hello");
hi
hello
My question is who is adding this extra \
before unescaping??
Sorry if it is a dumb question.
The JDBC driver does no escaping in the
ResultSet
. It would be very bad if it does. Look at this simple example:The database table
x
does contain one columnvalue
. There are two rows: One with a two character string ('\'
and'n'
) and one witch a one character string (the newline character). I've added a string-length to the output for clarification.This table is read with JDBC:
You see: no explicit escaping anywhere in the code so far. And the output is:
You see: There is still nothing escaped - neither by the code nor by JDBC.
BUT
Things get a bit murky if you mix in Java literals: If you do something like this:
then guess what happened? You have another row with one character!
This is because the Java compiler translated the two characters '\n' into one newline character. Hence the JDBC driver and the database only see one character.
But if you would have read some userinput and the user would have typed
\
andn
then nobody would de-escape this and the row would have contained two characters.Next step
You say, you do explicit de-escaping using
StringEscapeUtils
. Then this would happen:StringEscapeUtils
into a string containing one characters. After that both row look like the same to you.Summary
Do not confuse String-Literal escaping by the compiler with escaping by JDBC (which won't happen).
Do not add unnecessary escaping layers.
getString()
doesn't escape anything.If you have an EOL character (written as
"\n"
in a Java String literal, but being a single character in the string, whose numeric value is 10) in the string stored in the database, thengetString()
will return a String containing this character. Printing this String will result in two lines.If you have a
\
character followed by an
character (which would thus be written as"\\n"
in a Java String literal, but would only result in two characters in the string,\
andn
), thengetString()
will return a String containing these two characters. Printing this String will result in\n
to be printed.In short,
\n
is used to be able to add a newline character in a String literal, in Java source code. But this is translated by the compiler to a String containing just one newline character. Not to a String containing a backslash followed by an
. At runtime, these escape sequences don't exist anymore.