Results 1 to 20 of 23
- 03-03-2011, 11:51 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
PreparedStatements and escaped text
Hi all
I'm having a bit of a problem. It seems that the PreparedStatement.setString(int, String) method doesn't recognise escaped characters in text.
I was hoping to find some sort of other method in PreparedStatement that recognises the escaped characters. Maybe there is one in String but I can't see it either :o
I have a String containing 2 '\' characters in a row (e.g. "foo\\bar"). Obviously the first '\' escapes the second and with a normal Statement object this sort of String is recognised for what it is and the first '\' is removed (so "foo\bar" is inserted). Is there a way to be able to successfully pass that String through PreparedStatement.setString(int, String)?
I really don't want to have to go back to ordinary Statements.
- 03-03-2011, 12:00 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What is currently being inserted by PreparedStatement?
- 03-03-2011, 12:11 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
- 03-03-2011, 12:17 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
I just checked it on quote marks (foo\"bar\") and the PreparedStatement inserts it as intended: foo"bar"
Maybe it just doesn't work for "\\". Which is a problem.
- 03-03-2011, 12:21 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Are you hardcoding the String?
Because if "foo\bar" is coming from, say, a client then that shouldn't be a problem.
ETA: In fact, which db and which driver are you using?Last edited by Tolls; 03-03-2011 at 12:24 PM.
- 03-03-2011, 12:24 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Wait wait I think the problem might be with my own code. I have some methods that are supposed to be removing the escapes and it looks like they don't do it. Looks like it might just be my idiocy. Which is embarrassing but a relief. :o
I'll need to look into it but I will keep the thread open to let you guys know if PreparedStatement actually is the problem. I certainly hope not.
- 03-03-2011, 01:02 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
I would be surprised, I have to say.
And it would also be driver dependent...
- 03-03-2011, 02:14 PM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Yea the Statement definitely inserts differently to the PreparedStatement. Even when I don't use my own methods and just setup a simple straight PreparedStatement and a Statement they produce different results for the same String.
For example:
If I give it "foo\\\\bar" the PreparedStatement ends up inserting: "foo\\bar" (or at least that is what shows up when I query the table)
For the same String Statement ends up inserting "foo\bar".
This is totally nuts. Perhaps it has something to do with the fact that "foo\\\\bar" is cobbled together into a larger INSERT statement String?
- 03-03-2011, 02:16 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Again, which driver are you using?
Which db?
- 03-03-2011, 02:18 PM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
db = MySQL (5.1 I think but I'll check) EDIT: yes definitely 5.1
driver = JDBC5.1
- 03-03-2011, 02:20 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Also, the Statement will be taking:
"INSERT INTO tbl ("foo\\\\bar")"
This will be turned into SQL:
INSERT INTO tbl ("foo\\bar")
Which, depending on you db (this applies to MySQL) as:
INSERT INTO tbl ("foo\bar")
Which is correct.
For the PreparedStatement:
ps.setString(1, "foo\\\\bar");
Will result in:
INSERT INTO tbl ("foo\\bar")
since the slashes ('\\' is a single slash remember, so the 4 will result in 2) will be escaped.
- 03-03-2011, 02:21 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
What is JDBC 5.1?
- 03-03-2011, 02:28 PM #13
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
- 03-03-2011, 02:32 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
No, I mean what driver are you using?
MySQL connector?
The thing that actually does the work.
- 03-03-2011, 02:33 PM #15
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
So you're saying MySQL does it's own escape editing when it receives the String? So essentially when I use an ordinary Statement object the String with 4 '\' characters gets processed for escaped characters not once but twice? (the first processing in Java drops it to 2 then the second processing in MySQL drops it to 1)
But for the PreparedStatement only Java does escaped text processing? So it only drops to 2 '\'?
- 03-03-2011, 02:37 PM #16
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
- 03-03-2011, 02:49 PM #17
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Hmm I see what you mean. JDBC is a broad specification for connectors.
I checked up on it and it is MySQL Connector/J.
Sorry for the confusion.
- 03-03-2011, 02:57 PM #18
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Why we create the Dynamic Object in java ???
please anybody tell me about the full specification of that why we use this terminology in java..
even that we can create the object statically in c language ..and it is more memory using than dynamic created objects ....
Please Specify the answers ..
- 03-03-2011, 03:00 PM #19
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Uh... maybe make a thread for it? ... you know... your own thread...
- 03-03-2011, 03:06 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
All JDBC does is send the SQL statement down as it should appear (after escaping is taken into account).
Since '\' is the escape character for MySQL then anything arriving with that in will be escaped itself.
Using setString result in the PreparedStatement escaping what you provide.
So the Statement '\\\\' becomes '\\' for the SQL and then '\' in the database.
For the PreparedStatement the '\\\\' becomes '\\' for the SQL, which the PreparedStatement will escape back to '\\\\' which results in '\\' in the database.
Because if someone enters "some data I really want entered including all the slashes \\\\" into a field on whatever front end they're using, they will expect to see that in the db. With PreparedStatement they will get that, but with a Statement they'll only get two slashes.
Similar Threads
-
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
How display text in listbox or text area right to left or center??
By sameer22 in forum CLDC and MIDPReplies: 0Last Post: 09-28-2010, 09:52 AM -
PreparedStatements
By lompic in forum New To JavaReplies: 0Last Post: 04-14-2010, 12:59 PM -
BinaryStream with PreparedStatements
By atom86 in forum Advanced JavaReplies: 3Last Post: 10-21-2009, 11:54 AM -
how to use live validation with autocomplete in dojo text boxes in <s:text box>
By subashm28 in forum Suggestions & FeedbackReplies: 2Last Post: 01-23-2009, 04:09 PM


LinkBack URL
About LinkBacks


Bookmarks