• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About getGeneratedKeys()

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, somewhere in my program I try to get the generated keys of my insert statement. However, I set auto commit to false. Will getGeneratedKeys() work even when I set to autocommit false? Here's my sample code...




and then I use it like this...



Parent ID prints blank!!!? Thank you...
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, the getGeneratedKeys() is new in java 1.4; it could be your JDBC driver does not really implement this completely or properly yet.

It may be that this feature does not work with prepared statements ?

I was going to say to try to use the alternate signature for executeUpdate:
int executeUpdate(String sql,
int autoGeneratedKeys)
throws SQLException

the jdbc API docs say :

autoGeneratedKeys - a flag indicating whether auto-generated keys should be made available for retrieval; one of the following constants: Statement.RETURN_GENERATED_KEYS Statement.NO_GENERATED_KEYS



it could be your JDBC driver implementation uis by default not making the generated keys available when the executeUpdate() is called.

but that you need to pass it a query string, which defeats the good of using prepared statements.

hmmm...

would it be possible to have a mthod that invokes a sequence, or generates the keys, then you would pass that value into your insert statement:


hmm, the getGeneratedKeys() is new in java 1.4; it could be your JDBC driver does not really implement this completely or properly yet.

It may be that this feature does not work with prepared statements ?

I was going to say to try to use the alternate signature for executeUpdate:
int executeUpdate(String sql,
int autoGeneratedKeys)
throws SQLException

the jdbc API docs say :

autoGeneratedKeys - a flag indicating whether auto-generated keys should be made available for retrieval; one of the following constants: Statement.RETURN_GENERATED_KEYS Statement.NO_GENERATED_KEYS



it could be your JDBC driver implementation uis by default not making the generated keys available when the executeUpdate() is called.

but that you need to pass it a query string, which defeats the good of using prepared statements.

hmmm...

would it be possible to have a mthod that invokes a sequence, or generates the keys, then you would pass that value into your insert statement:

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would avoid using getGeneratedKeys() altogether (although I can greatly understand your desire to use it), it is barely supported by most drivers/dbs such that you can't really rely on. I think only got it to work once out of a number of tries.

Generating keys and using those values is a non-trivial matter. One solution (most common) is to always query for the max row after you create a record. If done as part of a transaction there are ways to make this somewhat safe.

Another solution I prefer is to not use database generated keys but generate the keys yourself. This probably has the best success since you never have to worry about someone else getting your key... if your insert fails (assuming the column is a key and/or unique) the transaction will roll back. Of course, there are ways of preventing that by using a static key generator.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic