posted 17 years ago
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.
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.
posted 17 years ago
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
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
