JDBC: Autogenerated keys revisited.
Some months ago i wrote about retrieving auto generated values with JDBC from an Oracle Database: JDBC: Get autogenerated keys on a Oracle DB.
The solution i presented in the previous article doesn’t run in a Oracle Java Stored Procedure.
To accomplish this, use a callable statement like this:
final String sql = "BEGIN INSERT INTO foobar(id, b) VALUES (id.nextval, ?) RETURNING id INTO ?; END;";
CallableStatement cs = connection.prepareCall(sql);
stmt.setString(1, "bar");
stmt.registerOutParameter(2, Types.INTEGER);
stmt.executeUpdate();
rv = rs.getInt(2); |
This way you get the id generated by the sequence id without first selecting and then using it.
Share This
Some months ago i wrote about retrieving auto generated values with JDBC from an Oracle Database: JDBC: Get autogenerated keys on a Oracle DB. The solution i presented in the...
— Trackback URI
This entry (permalink) was posted on Wednesday, April 2, 2008, at 2:44 pm by Michael, tagged with Code Snippets, Java, Oracle, Oracle DB, Tipps and categorized in English posts, Java.
The following post could be of some interest: JDBC: Get autogenerated keys on a Oracle DB, Spring und JNDI Datasources, How to get UIDefaults in Java, RFC3339 revisited, How to retrieve tables of custom object types with JDBC, Division by zero, Oracle, JBDC, CallableStatements and named parameters, Fixing hibernate “Cannot release connection” exception using DBCP and MySQL., Rails and Grails revisited, Oracle JDBC Driver und AspectJ
Post a Comment