|
 |
|
 |
 |
06-21-2004, 01:27 PM
|
#1 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
JDBC: Insert ID
When you have a database that you can setup to automatically increment an ID field, is there a way via JDBC to retrieve the value after an insert?
I'm using JDBC with DB2 and I'm not sure if I'm just sol on this one.
__________________
testing 1 2 3
|
|
|
06-21-2004, 02:39 PM
|
#2 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
You know, I never bothered looking up how to do this because I was able to solve my problem with an SQL query, but I think there is a way to do what you want. Use the execute(String sql, int autoGeneratedKeys) method. You can then call getGeneratedKeys(), which returns a ResultSet. Let me know if it works for you.
|
|
|
06-21-2004, 03:31 PM
|
#3 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
i found the getGeneratedKeys() method, but i can't seem to get it to work.
it does say jdbc3.0 , and i'm not sure what version of jdbc i'm on .. i'll try looking up how to find this out.
__________________
testing 1 2 3
|
|
|
06-21-2004, 03:42 PM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
Code:
Statement stmt = conn.createStatement();
// Obtain the generated key that results from the query.
stmt.executeUpdate("INSERT INTO authors " +
"(first_name, last_name) " +
"VALUES ('George', 'Orwell')",
Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
if ( rs.next() ) {
// Retrieve the auto generated key(s).
int key = rs.getInt(1);
}
here is a code example of what i need ..
i can't find the getGeneratedKeys() method off the Statement object for the life of me. =/
is the jdbc version related to the j2sdk version ? i'm on 1.4.2
__________________
testing 1 2 3
|
|
|
06-21-2004, 03:49 PM
|
#5 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
what do you mean you can't find it ?
does the code you've written compile ? it should ...
__________________
direct entry file specification.
|
|
|
06-21-2004, 03:52 PM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
yes i just did it as you posted that lol .. it works!! damn netbeans just doesn't recognize any of that.
i was relying on my IDE drop down stuff too much to check for validity.
__________________
testing 1 2 3
|
|
|
06-21-2004, 05:21 PM
|
#7 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Netbeans only seems to pickup on stuff after you compile, and often you need to hit enter on a line marked as an error before it goes away.
|
|
|
06-21-2004, 05:29 PM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
i had that figured out, .. but what about the feature when you type a dot after a class and it drops down with all the properties/methods available for that object? it is not showing everything. maybe i need to update netbeans.
__________________
testing 1 2 3
|
|
|
06-21-2004, 05:33 PM
|
#9 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Well, it won't show private methods and whatnot . . . I honestly don't use that function very often so I'm not sure why it's doing that.
|
|
|
06-21-2004, 06:23 PM
|
#11 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Rely on the API more, it's a bit easier to just look through the methods on an object that way.
|
|
|
06-21-2004, 06:56 PM
|
#12 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
yes, i do this too - i have the api downloaded so its a bit faster to view,
__________________
direct entry file specification.
|
|
|
06-21-2004, 08:23 PM
|
#13 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
thanks for the advice.
__________________
testing 1 2 3
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 08:51 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|