Reputation: 605
I am trying to execute an insert statement and after execution to obtain the generated id. So far I have made the query and the prepared statement.
This is part of the code: PreparedStatement preparedStatement = connetion.prepareStatement(query.toString(), new String[] {"id"});
After adding all the parameters I execute this:
preparedStatement.executeUpdate();
ResultSet resultSet = preparedStatement.getGeneratedKeys();
resultSet.next();
resultSet.getLong(1);
The exception that it throws is this:
java.sql.SQLException: Invalid argument(s) in call
at oracle.jdbc.driver.AutoKeyInfo.getNewSql(AutoKeyInfo.java:187)
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:4342)
at com.sirmabc.rfcollection.manager.FeeManagerJDBC.save(FeeManagerJDBC.java:50)
at com.sirmabc.rfcollection.test.FeeManagerJDBCTest.testSave(FeeManagerJDBCTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
A very important fact is that the code works fine with ojdbc7
, but as soon as I switch it to work with ojdbc8
this error occurs.
Any help would be much appreciated.
Upvotes: 2
Views: 1336
Reputation: 109593
In this simple case of one key, you could do:
preparedStatement = connection.prepareStatement(query.toString(),
statement.RETURN_GENERATED_KEYS);
A new int[] {1}
looks like requiring the ID to be at the first column.
Such it-works solutions might become problematic in the far future.
Unfortunately that the String[]
version no longer seems to work.
Upvotes: 1
Reputation: 605
Obviously the method prepareStatement(query.toString(), new String[] {"id"});
no longer works for oracle databases. After long hours of bumping my head I found out that the method prepareStatement(query.toString(), new int[] {1});
works fine. The thing is you need to be careful when using it in oracle, it starts with 1 not with 0 if you put 0 it would again throw an exception for wrong arguments.
Upvotes: 2