Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC and the fly likes how to construct a sql query when field having single quote    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to construct a sql query when field having single quote    " Watch "how to construct a sql query when field having single quote    " New topic
Author

how to construct a sql query when field having single quote

zimbu bolleddu
Ranch Hand

Joined: Jan 14, 2008
Posts: 35
Hi all,

I have been working on web application , here is my requirement:

I'm constructing sql statement dynamically from dynamic user input (form data). In one of the field having single quote.

while executing the query it is getting problem because of single quote .. so how do i resolve my problem.

single quote should be there. (I'm using Ms-Access as my database).

Thanks in advance
zimbu
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2713
    
    5

Please CarefullyChooseOneForum for your post.This ensures desired response for your post.


SCJP, SCWCD.
|Asking Good Questions|
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7

Originally posted by Amit Ghorpade:
Please CarefullyChooseOneForum for your post.This ensures desired response for your post.


Moving to JDBC forum.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Are you using a PreparedStatement or passing the text directly? If you have the text directly you may need to convert the ' to \'. I don't know whether PreparedStatements will automatically escape the ' for youi.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Campbell Ritchie:
I don't know whether PreparedStatements will automatically escape the ' for you.


Yes , It will escape the single quote (').

Shailesh


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Originally posted by Shailesh Chandra:
Yes , It will escape the single quote (').

Shailesh
Thank you, and that's good. I hoped that is what it did, but thought I had better say "don't know" because I wasn't sure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to construct a sql query when field having single quote
 
Similar Threads
select field, count(field)... in EJB-QL
Correct sintax to order dependent objects
ResultSet problem
Handling data carry between different pages - Design handliing.
Use custom query on JPA