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 Passing quotes as literal characters. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Passing quotes as literal characters." Watch "Passing quotes as literal characters." New topic
Author

Passing quotes as literal characters.

Craig Snowbarger
Ranch Hand

Joined: Mar 07, 2001
Posts: 38
I'm trying to place quotes around a parameter in a Query string that uses a WHERE clause, but java won't compile. Is there any way to pass a quote as a literal string?
Here is the SQL statement that I want to use in my Java code:
SELECT Equipment.Make
FROM Equipment
WHERE (((Equipment.TestSetID)="TS1"));
Thanks in advance.
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
Craig,
It's possible to accomplish what you want using quotes (") and apostrophes ('), but my recommendation would be to avoid using them completely, which you can do if you use a PreparedStatement instead of a Statement. Here's an example of how easy it is:

The nice thing about this approach is that you don't need to use any quotation marks or apostrophes at all, and therefore don't need to ensure that they're "balanced" so that your SQL statement will be valid. For example, the above code will work even if it contained something like the following (note the embedded apostrophe in "company's"):

As you've seen, if you try to embed the quotation marks / apostrophes yourself, things can get a little complicated and confusing. That's why I'd recommend this approach instead.
------------------
Brett Spell
Author, Professional Java Programming
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing quotes as literal characters.