This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!