This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes getGeneratedKeys() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "getGeneratedKeys()" Watch "getGeneratedKeys()" New topic
Author

getGeneratedKeys()

Sajan Patel
Ranch Hand

Joined: Oct 25, 2004
Posts: 77
Dear All,
I am trying to retrieve IDENTITY from SQL Server Database. Up to now I was using SP so I using registerOutParameter(1, Types.INTEGER) method. But now I have to use prepared statement.

I have one table in SQL Server database which has one table where IDENTITY is created itself and IDENTITY_INSERT is set to off so no one can insert from outside


// There is another column also name PERSON_ID
String sqlPerson = "INSERT INTO PERSON(NAME, LASTNAME) VALUES (?,?)";

PreparedStatement pstmnt = con.prepareStatement(sqlPerson, Statement.RETURN_GENERATED_KEYS);

pstmnt.setString(1, "Any Name");
pstmnt.setString(2, "Any Last Name");

pstmnt.execute();

ResultSet rs2 = pstmnt.getGeneratedKeys();
int intID = -1;
while(rs2.next()){
intID = rs2.getInt(1);
}

I am having error of java.lang.AbstractMethodError.

I am not sure where is problemm according to me it is right but I am getting error means some thing is wrong.

Thought one
Do I have to mentioin something in sqlPerson like PERSON_ID column name or SET @@IDENTITY ?
Thought two
In Callable Statement we have to register return data and data type do we have to mention something like that some where?

thanks in advance
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getGeneratedKeys()