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?
 
objectsCommented:
> 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.
0
 
objectsCommented:
can u post your java code

best way to hable sql is to use a PreparedStatement

http://javaalmanac.com/egs/java.sql/InsertPs.html
0
 
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);
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

BTW, what is id? Number or String?
0
 
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.
0
 
objectsCommented:
> 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Any reason for the C grade?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.