简体   繁体   English

如何在没有 ArrayIndexOutOfBoundsException 的情况下通过 executeBatch 获取生成的键?

[英]How to get generated keys by executeBatch without ArrayIndexOutOfBoundsException?

The following method I want to INSERT several records simultaneously.下面的方法我想同时插入多条记录。

public void insert() {
    try {
        this.connection.setAutoCommit(false);
        PreparedStatement ps = this.connection.prepareStatement(
                "INSERT INTO  COMPANY (NAME,Address) Values (?,?)", new String[]{"ID"});
        ps.setString(1, "X01");
        ps.setString(2, "Address1");
        ps.addBatch();

        ps.setString(1, "Y01");
        ps.setString(2, "Address2");
        ps.addBatch();

       //EXCEPTION OCCURS HERE
        int[] numUpdates = ps.executeBatch();

        for (int i = 0; i < numUpdates.length; i++) {
                System.out.println("Execution " + i +
                        "successful: " + numUpdates[i] + " rows inserted");
        }

        ResultSet resultSet =
                (ps).getGeneratedKeys();
        while (resultSet.next()) {
            String deptNoKey = resultSet.getString(1);
            System.out.println("Automatically generated key value = "
                    + deptNoKey);
        }
    } catch (BatchUpdateException b) {
        // process BatchUpdateException
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

At this point, when I expect to get the PK's generated for each INSERT, I get this Exception在这一点上,当我希望为每个 INSERT 生成 PK 时,我得到了这个异常

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 22 at oracle.jdbc.driver.T4CNumberAccessor.unmarshalOneRow(T4CNumberAccessor.java:250) at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:754) at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216) at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1250) at oracle.jdbc.driver.OraclePreparedStatement.executeForRowsWithTimeout(OraclePreparedStatement.java:14264) at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:14379) at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:589) at dbpro.SqlHelper.insert2(SqlHelper.java:988) at dbpro.SqlHelper.main(SqlHelper.java:1023) 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:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

After Exception: In The table COMPANY , two records are added correctly but I expected to get a ResultSet with one row for each insert performed, so I could get each PK generated.异常之后:在表 COMPANY 中,正确添加了两条记录,但我希望得到一个 ResultSet ,每次执行插入时都有一行,因此我可以生成每个 PK。

This appears to be a bug in Oracle on Windows, the driver JAR ojdbc6.jar, or (knowing Oracle) both.这似乎是 Windows 上的 Oracle、驱动程序 JAR ojdbc6.jar 或(知道 Oracle)两者中的错误。

There's no significant problem with the code you have presented.您提供的代码没有重大问题。 It should work, although when I ran it the numbers of rows inserted each time was returned as -2 (= Statement.SUCCESS_NO_INFO ), so you may be better off ignoring these numbers.它应该可以工作,尽管当我运行它时,每次插入的行数返回为 -2(= Statement.SUCCESS_NO_INFO ),因此您最好忽略这些数字。

Your code runs fine for me with Oracle 11g XE 11.2.0.2.0 on Linux with four versions of the Oracle JDBC driver JAR.您的代码在 Linux 上使用 Oracle 11g XE 11.2.0.2.0 和四个版本的 Oracle JDBC 驱动程序 JAR 运行良好。 However, if I run it on Windows 10 with the same version of Oracle XE and with ojdbc6.jar, it fails with the same ArrayIndexOutOfBoundsException you are getting.但是,如果我在具有相同版本的 Oracle XE 和 ojdbc6.jar 的 Windows 10 上运行它,它会失败并出现与您相同的 ArrayIndexOutOfBoundsException。 The problem goes away if I use ojdbc7.jar instead of ojdbc6.jar.如果我使用 ojdbc7.jar 而不是 ojdbc6.jar,问题就会消失。

Therefore, I would recommend replacing ojdbc6.jar with ojdbc7.jar, which you can download from here .因此,我建议将 ojdbc6.jar 替换为 ojdbc7.jar,您可以从这里下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM