Home > Archive > SQL Server JDBC > April 2005 > Retrieving generated keys









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Retrieving generated keys
garfield13

2005-04-07, 8:01 pm

I have the following scenario:

MyTable
id number entity(1, 1)
name nvarchar

sniplet of Java code:
String sql = "Insert Into MyTable (name) Values( 'MyName' )";
int updateCnt = stmt.executeUpdate(sql, Statement. RETURN_GENERATED_KEY
S);
ResultSet rs = stmt.getGeneratedKeys();

The executeUpdate() method throws an java.lang.AbstractMethodError exception.

How do I get the value of "id" after running my insert statement?

The microsoft example goes through a lot of object instantiation with
prepared calls and all that I don't necessarily want to complicate the
previous code by doing all that. But if I do, could someone please help me
with the conversion? I always use the above approach.

Thanks.
Alin Sinpalean

2005-04-07, 8:01 pm

The MS driver is a JDBC 2.0 implementation.
Statement. RETURN_GENERATED_KEY
S is a JDBC 3.0 feature. Either use a
JDBC 3.0 driver or append SELECT SCOPE_IDENTITY() to all your INSERT
statements.

Alin,
The jTDS Project.

Jimbo

2005-04-14, 11:23 am

If youre using SQL Server why not do the insert through a stored
procedure?...this way you can use the Scope_Identity function to return
back the value of the newly inserted identity to Java..let me know if
you need some sample code...

Alin Sinpalean

2005-04-14, 11:23 am

Jimbo wrote:
> If youre using SQL Server why not do the insert through a stored
> procedure?...this way you can use the Scope_Identity function to

return
> back the value of the newly inserted identity to Java..let me know if
> you need some sample code...


You can use scope_identity() without a stored procedure. Just execute
something like "INSERT ... SELECT scope_identity()". The problem is
that the JDBC code won't work across databases.

Alin.

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com