Hi. I have been trying to retrieve the value of an auto-generated key. Below is a snippet of code that, according to the JDBC documentation, should work:
----
String sqlCmd2 = "INSERT INTO GLAD (ACN, TYPE, DESC1) VALUES (GLAD.NEXTVAL, 'A', 'New General Ledger Account')";
out.println(sqlCmd2);
stmt = con.createStatement();
OK = stmt.executeUpdate(sqlCmd2, Statement.RETURN_GENERATED_KEYS);
results = stmt.getGeneratedKeys();
long newAcn;
if (results.next()) {
newAcn = results.getLong(1);
out.println("<br />values returned = " + String.valueOf(newAcn));
}
out.println("Update result = " + OK);
results.close();
stmt.close();
con.close();
----
Here is the resulting output. The insertion of the record did succeed and the correct next key value was generated:
----
INSERT INTO GLAD (ACN, TYPE, DESC1) VALUES (GLAD.NEXTVAL, 'A', 'New General Ledger Account')
values returned = 0 Update result = 1
----
This a feature I really need. Any ideas about why this isn't working?
Ed
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi. I have been trying to retrieve the value of an auto-generated key. Below is a snippet of code that, according to the JDBC documentation, should work:
----
String sqlCmd2 = "INSERT INTO GLAD (ACN, TYPE, DESC1) VALUES (GLAD.NEXTVAL, 'A', 'New General Ledger Account')";
out.println(sqlCmd2);
stmt = con.createStatement();
OK = stmt.executeUpdate(sqlCmd2, Statement.RETURN_GENERATED_KEYS);
results = stmt.getGeneratedKeys();
long newAcn;
if (results.next()) {
newAcn = results.getLong(1);
out.println("<br />values returned = " + String.valueOf(newAcn));
}
out.println("Update result = " + OK);
results.close();
stmt.close();
con.close();
----
Here is the resulting output. The insertion of the record did succeed and the correct next key value was generated:
----
INSERT INTO GLAD (ACN, TYPE, DESC1) VALUES (GLAD.NEXTVAL, 'A', 'New General Ledger Account')
values returned = 0 Update result = 1
----
This a feature I really need. Any ideas about why this isn't working?
Ed
Is anyone looking at this problem?
Ed
I'm looking into this problem. I'll let you know as soon as I solve this.
Thanks
Manoj