Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 06-21-2004, 01:27 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-21-2004, 02:39 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 06-21-2004, 03:31 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-21-2004, 03:42 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-21-2004, 03:49 PM   #5 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
what do you mean you can't find it ?

does the code you've written compile ? it should ...
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-21-2004, 03:52 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-21-2004, 05:21 PM   #7 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 06-21-2004, 05:29 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-21-2004, 05:33 PM   #9 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 06-21-2004, 05:36 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
http://java.sun.com/j2se/1.4.2/docs/...tGeneratedKeys()

it's a public method. i am dependant on the drop down help .. for now anyway.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 06-21-2004, 06:23 PM   #11 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Rely on the API more, it's a bit easier to just look through the methods on an object that way.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 06-21-2004, 06:56 PM   #12 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
yes, i do this too - i have the api downloaded so its a bit faster to view,
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-21-2004, 08:23 PM   #13 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
thanks for the advice.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error SQL-query in importing file infinite_root PHP 1 06-08-2004 05:17 AM
JDBC and AS400/iSeries Connection with JTOpen sde Java 0 05-17-2004 09:42 AM
MYSQL - insert jimmyoctane PHP 7 09-15-2003 01:15 PM
HTML form preview then INSERT using PHP & MySQL SteveSoler PHP 5 07-07-2002 09:54 AM


All times are GMT -8. The time now is 08:51 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle