*
The moose likes JDBC and the fly likes inserting single quotes into database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserting single quotes into database" Watch "inserting single quotes into database" New topic
Author

inserting single quotes into database

vikas sharmaa
Ranch Hand

Joined: Jun 28, 2007
Posts: 191
i am getting "missing comma" SQL error while inserting a String that contains single quotes in it. for example, the below insert statement code:



now, the above code will throw run time sql error because the insert statement has single quotes in manager's word as shown below:

insert into emp (detail) values ('its manager's duty.')

i guess this will be a very common problem. so, is there any standard way or in-built method to resolve the issue. please help. thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
Try 'The manager''s duty'
Try 'The manager\' duty'

Note first example is single quote twice, not double quote.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Or better still, use a PreparedStatement:

That way you don't need to worry about quotes. Or date formatting, or any other SQL formatting issues. The JDBC driver takes care of all of the escaping for you.
vikas sharmaa
Ranch Hand

Joined: Jun 28, 2007
Posts: 191
thank you campbell and paul.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
You're welcome
 
wood burning stoves
 
subject: inserting single quotes into database
 
Similar Threads
What exactly we mean by Precompiled SQL Statement
Problem inserting into DB in a JSP
Prepared statement's 'setDate' method
invalid character constant
INSERT Issue