• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Techniques for getting auto generated key for ms access?

 
Joshua White
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning all,

When inserting a row into access, the resultSet.getGeneratedKeys method returns a UnsupportedOperationException. Are there any work around techniques for obtaining the autogenerated key?

Regards,

Joshua
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using Access? Then just use an autonumber datatype and exclude the column from your insert statement. Access will create the key.
[ January 05, 2005: Message edited by: Paul Sturrock ]
 
Joshua White
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Inserting is not the problem. After I successfully perform an insert, I am trying to obtain the generated key. Calling the following method after performing an insert works, but I was hoping there was a better way...

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, read your post too quick. Unfortunately if the driver does not support the method, then your workaround is your only option.
 
Ken McCafferty
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just learning Java but have experience using Access with Cold Fusion.

Getting the last inserted PK can be a bit tricky. One way is to write a SELECT query using the MAX function. Immediately after doing the insert, the SELECT query is run. This will work if only one user is using the DB.

But in a situation where multiple users are accessing the DB(such as the web), this method can give the wrong results.

A method that does work is to insert a unique ID into the DB when doing the insert and then use this value in the SELECT query. I am new to Java and do not know whether this is a function to generate a unique ID.

Ken
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suugestion is the same as Paul's. Create your own method which returns you the unique key everytime. But again this should not be based on the existing keys in your table. You can go for timestamp.

cheers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic