File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes UnsupportedOperationException Statement.getGeneratedKeys() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "UnsupportedOperationException Statement.getGeneratedKeys()" Watch "UnsupportedOperationException Statement.getGeneratedKeys()" New topic

UnsupportedOperationException Statement.getGeneratedKeys()

David Weber

Joined: Nov 03, 2009
Posts: 9
Hi guys I am trying to retrieve the auto_incremented value from a MySQL Insert Statement,
And I keep getting the following exception:

I have googled and googled and so far no fix, have tried prepared statements as well as statements.

My code:

Any help will be great guys!, thanks in advance.

Extra Info:

Windows 7 32-bit
NetBeans 6.7
MySQL 5.1
MySQL ODBC Connecter 5.1.5

Regards, David
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

I love error messages like these because its pretty clear what the problem is. In this case "UnsupportedOperationException" means your JDBC driver does not support this feature. Retrieving generated keys is NOT a standard feature nor is it supported by all drivers. Your only choices are to switch to a different driver (often there isn't one), switch to a different database (thats rarely an option), or find another way programmatically to deal with the problem. I actually wrote an article on this some time back: Database Key Generation in Java Applications

[OCA 8 Book] [Blog]
David Weber

Joined: Nov 03, 2009
Posts: 9
Thanks Scott,

My mistake was using the standard jdcb.odbc.driver instead of the mysql.jdbc.driver

Problem solved!
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

The native MySQL jdbc driver for your MySQL version supports it.
If you stop using the odbc bride, and start using the jdbc driver, your code will work.

Regards, Jan

(Edit: I managed to miss the last post in the thread. Glad it's solved.)

OCUP UML fundamental and ITIL foundation
I agree. Here's the link:
subject: UnsupportedOperationException Statement.getGeneratedKeys()
It's not a secret anymore!