File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 40064
    
  28
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: 18993
    
    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: 40064
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inserting single quotes into database