?
Solved

Strange SQL Command Behaviour in Java App

Posted on 2006-04-02
15
Medium Priority
?
240 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.
0
Comment
Question by:alex_ge
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16357689
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16358022
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16358040
>> If I execute it in MS Access SQL window directly, it works

Then probably there's something wrong with the query-building part.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:hoomanv
ID: 16358169
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
 

Author Comment

by:alex_ge
ID: 16358266
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16358282
>> 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
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 16358288
> 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
 

Author Comment

by:alex_ge
ID: 16358477
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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 16358519
>> 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
 
LVL 92

Expert Comment

by:objects
ID: 16358594
that is strange, never seen that before.
What if you used escaped double quotes around the string instead of dingle quotes?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16358682
>> WHERE id = 1

BTW, what is id? Number or String?
0
 

Author Comment

by:alex_ge
ID: 16358891
>>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
 
LVL 92

Expert Comment

by:objects
ID: 16358907
> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16367662
Any reason for the C grade?
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question