I am working in JEE, with WebLogic and OracleXE as database engine, I am using a DataSource (NOT JPA), in this database sequences must be created to generate autoincremental values, I have already done that and it works fine for me when I do it. I execute since it also creates a trigger to generate the value automatically, although that is another topic.
From my application I can insert data to the database, but I need to obtain the value of that ID that I mentioned in the previous paragraph.
I already executed this example that I took from the internet and it doesn't work for me, it doesn't even insert the values:
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate....) VALUES (1, 1...) RETURNING OrderID INTO :last_insert_id
Any other suggestions?
Here a bit of code
> PreparedStatement sttmt = conn.prepareStatement("INSERT INTO fabricantes (NOMBRE, PAIS) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS);
String nombre = "Colo Colo";
String pais = "Chile";
sttmt.setString(1, nombre);
sttmt.setString(2, pais);
int resultado = sttmt.executeUpdate();
// ResultSet rs = sttmt.getGeneratedKeys();
Long id = null;
ResultSet rs = sttmt.getGeneratedKeys();
if (rs.next()) {
id = rs.getLong(1);
// Long id = generatedKeys.getLong(1);
}
return id;
The table also has a data COD_FABRICANTE
that is not seen in the embed code because it is generated automatically, it is the primary key that I want to obtain precisely. The value of COD_FABRICANTE
is generated autoincrementally through a sequence that I create, in the Oracle 11g database there is no value IDENTITY
or value SERIAL
like in other Databases. Due to WebLogic Server restriction, I am using JDK 6, the platform does not support higher JDKs.
One way that should work correctly is by surrounding your statement
INSERT
withBEGIN ... END;
so that it becomes a PL/SQL block. This should allow the use of the clauseRETURNING
.Then you use a
CallableStatement
to be able to register an output parameter.Code example:
By
jdbc
and with oneinsert
withoutreturning
: