aspose file tools*
The moose likes JDBC and the fly likes Escape characters in JDBC (single quote/apostrophe) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Escape characters in JDBC (single quote/apostrophe)" Watch "Escape characters in JDBC (single quote/apostrophe)" New topic
Author

Escape characters in JDBC (single quote/apostrophe)

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
My java code works fine as long as the field does not contain a single quote. Since the strings are wrapped in single quotes when sent to the DBMS, strings containing single quotes mess up the statement.
I have created a pseudo example, removing much of the code, leaving only what I think is important for this question:
------------
String name_var = "O'Maley";
query_smt = "insert name_table(the_name) values('" + name_var + "'";
try
{
ResultSet rset = m_statement.executeQuery(query_smt);
}
catch(java.lang.Exception e)
{
....
}
------------
Please, any help on how I can pass the string with the single quote to the DBMS would be greatly appriciated.
(I am using Sybase)
MP
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Escape the single quote with another single quote:
String name_var = "O''Maley";

query_smt = "insert name_table(the_name) values('" + name_var + "'";
The above will insert "O'Maley".
[This message has been edited by Bhupinder Dhillon (edited March 08, 2001).]
[This message has been edited by Bhupinder Dhillon (edited March 08, 2001).]
Anthony Cunningham
Greenhorn

Joined: Jul 13, 2001
Posts: 4
or use prepared statement, it seems not to have a problem with '
 
jQuery in Action, 2nd edition
 
subject: Escape characters in JDBC (single quote/apostrophe)