ArrayIndexOutOfBoundsException when use AutoGenera

2019-07-23 23:35发布

问题:

I use ojdbc6-11.1.0.7.0 and when:

ps = connection.prepareStatement("Insert into A (b, C) values (?, ?)", Statement.RETURN_GENERATED_KEYS);

I get

java.lang.ArrayIndexOutOfBoundsException: 3
    at oracle.jdbc.driver.OracleSql.computeBasicInfo(OracleSql.java:950)
    at oracle.jdbc.driver.OracleSql.getSqlKind(OracleSql.java:623)
    at oracle.jdbc.driver.OraclePreparedStatement.<init>(OraclePreparedStatement.java:1212)
    at oracle.jdbc.driver.T4CPreparedStatement.<init>(T4CPreparedStatement.java:28)
    at oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:68)
    at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:3140)
    at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:3042)
    at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:5890)

I just read about this bug here, but solution does not help because I use Statement.RETURN_GENERATED_KEYS feature.

Is there any other solution?

回答1:

Ok at this time we make workarount:

  • insert without Statement.RETURN_GENERATED_KEYS
  • because we use sequence to generate ids add additional query that:

    select seq.currval from dual
    

That works fine but we dont have many users and this solution dont protect against multithread problems (we can get wrong Id).



回答2:

try ps = connection.prepareStatement("Insert into A (b, C) values (?, ?)", Statement.RETURN_GENERATED_KEYS);



回答3:

Please use ojdbc6.jar (rather than ojdbc6_g.jar) from oracle. It will solve this problem.



标签: java oracle jdbc