• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Passing quotes as literal characters.  RSS feed

Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!