alex_ge
asked on
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.
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.
Try
INSERT INTO memos (name, content, date_entered, last_modified, type, created_by, group_id) VALUES ('12', 'anything for \u0075\u0070\u0064\u0061\u 0074\u0065 ', #2006-04-03 11:44:14#, #2006-04-03 11:44:14#, 0, 1, 0);
INSERT INTO memos (name, content, date_entered, last_modified, type, created_by, group_id) VALUES ('12', 'anything for \u0075\u0070\u0064\u0061\u
>> If I execute it in MS Access SQL window directly, it works
Then probably there's something wrong with the query-building part.
Then probably there's something wrong with the query-building part.
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
http://java.sun.com/docs/books/tutorial/jdbc/jdbc2dot0/sample.html
ASKER
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
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
>> 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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
The String is properly placed in between single quotes. In its simplest form, the code is:
stmt.executeUpdate("UPDATE
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
that is strange, never seen that before.
What if you used escaped double quotes around the string instead of dingle quotes?
What if you used escaped double quotes around the string instead of dingle quotes?
>> WHERE id = 1
BTW, what is id? Number or String?
BTW, what is id? Number or String?
ASKER
>>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.
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.
> 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.
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.
Any reason for the C grade?
best way to hable sql is to use a PreparedStatement
http://javaalmanac.com/egs/java.sql/InsertPs.html