Strange SQL Command Behaviour in Java App

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.
alex_geAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
can u post your java code

best way to hable sql is to use a PreparedStatement

http://javaalmanac.com/egs/java.sql/InsertPs.html
CEHJCommented:
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 SAssociate Director - Product EngineeringCommented:
>> If I execute it in MS Access SQL window directly, it works

Then probably there's something wrong with the query-building part.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

hoomanvCommented:
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
alex_geAuthor 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 SAssociate Director - Product EngineeringCommented:
>> 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?
Mick BarryJava DeveloperCommented:
> 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alex_geAuthor 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 SAssociate Director - Product EngineeringCommented:
>> 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 DeveloperCommented:
that is strange, never seen that before.
What if you used escaped double quotes around the string instead of dingle quotes?
Mayank SAssociate Director - Product EngineeringCommented:
>> WHERE id = 1

BTW, what is id? Number or String?
alex_geAuthor 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 DeveloperCommented:
> 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 SAssociate Director - Product EngineeringCommented:
Any reason for the C grade?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.