We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Strange SQL Command Behaviour in Java App

alex_ge
alex_ge asked
on
Medium Priority
289 Views
Last Modified: 2010-03-31
Hi,

Ran into an error when executing the following sql command in my Java app:

INSERT INTO memos (name, content, date_entered, last_modified, type, created_by, group_id) VALUES ('12', 'anything for update', #2006-04-03 11:44:14#, #2006-04-03 11:44:14#, 0, 1, 0). I'm using MS Access Database. Managed to narrow down to the 'anything for update' part. It seems that whennever I have "for update" there, with any text and a space in front, I'll have this error, "Optional feature not implemented". If I execute it in MS Access SQL window directly, it works. I'm wondering whether this is a bug in Java implementation. Please advise.

Thanks.
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
can u post your java code

best way to hable sql is to use a PreparedStatement

http://javaalmanac.com/egs/java.sql/InsertPs.html
CERTIFIED EXPERT
Top Expert 2016

Commented:
Try

INSERT INTO memos (name, content, date_entered, last_modified, type, created_by, group_id) VALUES ('12', 'anything for \u0075\u0070\u0064\u0061\u0074\u0065', #2006-04-03 11:44:14#, #2006-04-03 11:44:14#, 0, 1, 0);
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> If I execute it in MS Access SQL window directly, it works

Then probably there's something wrong with the query-building part.
Top Expert 2006

Commented:
I also had such a problem with "insert into" and I never found its reason. however i tried to use updatable result sets and that worked for me. i just made a call to select one row (just to get the table structre not the row itself) and then i used the api methods to insert a new row instead of calling and insert into sql statement. it may help you.
http://java.sun.com/docs/books/tutorial/jdbc/jdbc2dot0/sample.html

Author

Commented:
To objects and mayankeagle: it's a simple sql insert statement. I can confirm nothing wrong with the code or the query-building. I have tried many times and confirmed that the exception is thrown only when the String contains "for update", with any other text and a space in front.

To CEHJ: No problem, it works. Because you have replaced "update" with its unicode equivalent.

My guess is that "for update" is a reserved keyword in Access and it is not properly handled in Java. I need some professional opinion on this and preferably advice on a proper way to handle this situation

thanks
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> I can confirm nothing wrong with the code or the query-building

That's fine :) can you please still post it for our understanding?

>> My guess is that "for update" is a reserved keyword in Access

That is correct. Update is a keyword in SQL as we all know.

>> it is not properly handled in Java

That's where the problem is - it should be. Perhaps somehow the "for update" is coming out of the '' (single quotes) - we need the query to analyze it. Did you also try PreparedStatement?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
> My guess is that "for update" is a reserved keyword in Access and it is not properly handled in Java.

PreparedStaements handle those types of issues

It mat be related to how you are quoting for eaxmaple.

> I need some professional opinion on this and preferably advice on a proper way to handle this situation

Without seeing your code its hard to say.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
To mayankeagle: we are talking about "For Update" as a combined keyword, not just "Update".

The String is properly placed in between single quotes. In its simplest form, the code is:
stmt.executeUpdate("UPDATE memos SET content = 'anything for updateanything' WHERE id = 1"); where stmt is a Statement. You can replace "anything" with any text. Note that in front of the second "anything", with or without the space, it's the same effect.

Based on you guys' advice, I tried out PreparedStatement, yes,  that does the trick. Thanks.

So, to solve this problem, we can simply replace Statement with PreparedStatement. For your information, you can also try to insert one more space in between "for" and "update", though not professionally, it works.

Afterthought: I'm confused here. In our app, the text "anything for update" could appear in any SQL update String. For our app to be robust, we cannot use Statement. Then what is the use of it? We may as well totally abandon it and stick to PreparedStatement.Any thought on this?
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
>> To mayankeagle: we are talking about "For Update" as a combined keyword, not just "Update".

That's all right - basically there is a keyword there and it is causing problems.

>> The String is properly placed in between single quotes. In its simplest form, the code is:

The simplest form is not what we require :) probably something wrong with this specific statement.

>> Based on you guys' advice, I tried out PreparedStatement, yes,  that does the trick. Thanks.

Yes, it generally always does and is more recommended. It will also improve performance if you run the same query multiple times (like say, in a loop) but with different values.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
that is strange, never seen that before.
What if you used escaped double quotes around the string instead of dingle quotes?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> WHERE id = 1

BTW, what is id? Number or String?

Author

Commented:
>>What if you used escaped double quotes around the string instead of dingle quotes?
Tried. Nope, does not work. Actually by no means should double quotes work right? That will generate a syntax error.

>>BTW, what is id? Number or String
It's a number

Maybe you guys can try it out on your machine. Simple build an Access db and write a test class. You should be able to replicate the error in 10 minutes.

Really strange huh. Maybe nobody ever used 'anything for update anything' in a Java text input, hence this problem had never been reported.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Actually by no means should double quotes work right? That will generate a syntax error.

depends what the driver does when generating the query.
One of the main reasons I use PreparedStatement as it lets the driver handle mapping the actual argumanet values to the syntax expected by the db.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Any reason for the C grade?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.