Re: getGeneratedKeys( ) Problem in JSF
Mongoose wrote:
I am trying to retrieve the primary key from a newly inserted row in
an Oracle Database from a JSF application. However, when I execute
this line:
Integer x = rs1.getInt("PurchaseID");
in the code below I get an SQLException "Unsupported Feature". I'm
using Oracle Database 10g and ojdbc14.jar.
Could be a jdbc driver problem but I'm not sure. Could someone help
me out here??
...
Statement stmt2 = conn.createStatement();
SQLStatement1 = new StringBuffer("INSERT INTO Purchase (PurchaseID,
CarModelID) VALUES (");
SQLStatement1.append("PurchaseID_Seq.nextval, ");
SQLStatement1.append(ModID + ")");
"SQL injection attack" - learn from 7-11 and others.
<http://xkcd.com/327/>
Use 'PreparedStatement' instead of dynamic SQL.
stmt2.executeUpdate(SQLStatement1.toString(),
Statement.RETURN_GENERATED_KEYS);
ResultSet rs1 = stmt2.getGeneratedKeys();
if (rs1.next())
{
Integer x = rs1.getInt("PurchaseID");
}
Does "UnsupportedFeature" apply to 'RETURN_GENERATED_KEYS'? Perhaps
the Oracle Java 5 or Java 6 JDBC drivers have that feature. Try them
instead of the Java 1.4 version.
Normally you don't need to explicitly insert a 'nextval' into a
generated ID. Perhaps you should use the "INSERT ... RETURNING
PurchaseID" form of insert.
--
Lew