本文介绍了JDBC批处理INSERT,返回ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以使用RETURNING INTO获取受影响的行的值?我必须将相同的行插入x次并获取插入的行的ID.

is there any way to get the values of affected rows using RETURNING INTO ?I have to insert the same rows x times and get the ids of inserted rows.

查询如下所示:

public static final String QUERY_FOR_SAVE =
        "DECLARE " +
           " resultId NUMBER ; " +
        "BEGIN " +
           " INSERT INTO x " +
           " (a, b, c, d, e, f, g, h, i, j, k, l, m)  " +
           " values (sequence.nextVal, :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l) " +
           " RETURNING a INTO :resultId;" +
        "END;";

现在,我可以使用addBatch在JAVA循环中将此查询添加到批处理中

Now i can add thise query to batch, in JAVA loop using addBatch

            IntStream.range(0, count)
                .forEach(index -> {
                    try {
                        setting parameters...
                        cs.addBatch();

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                });
        cs.executeBatch();

有没有办法像这样从批处理中返回数组或列表?我可以仅使用sql来执行x次插入操作,但在这种情况下,我还想知道如何返回ID数组.

Is there any way to return an array or list from batch like this ?I can execute those insert x times using just sql but in this case i also wondering how to return an array of ids.

预先感谢

推荐答案

我假设这与Oracle有关.据我所知,这是不可能的,但是您可以在匿名PL/SQL块中使用 FORALL 运行批量插入,如我最近写的这篇文章中所述: https://blog.jooq.org/2018/05/02/how-to-run-a-bulk-insert-returning-statement-with-oracle-and-jdbc/

I'm assuming this is about Oracle. To my knowledge, this isn't possible, but you can run a bulk insertion using FORALL in your anonymous PL/SQL block, as described in this article I wrote, recently:https://blog.jooq.org/2018/05/02/how-to-run-a-bulk-insert-returning-statement-with-oracle-and-jdbc/

这是本文中的一个自包含的JDBC示例,该示例插入值数组并批量将结果收集回JDBC客户端:

This is a self-contained JDBC example from the article that inserts an array of values and bulk collects the results back into the JDBC client:

try (Connection con = DriverManager.getConnection(url, props);
    Statement s = con.createStatement();

    // The statement itself is much more simple as we can
    // use OUT parameters to collect results into, so no
    // auxiliary local variables and cursors are needed
    CallableStatement c = con.prepareCall(
        "DECLARE "
      + "  v_j t_j := ?; "
      + "BEGIN "
      + "  FORALL j IN 1 .. v_j.COUNT "
      + "    INSERT INTO x (j) VALUES (v_j(j)) "
      + "    RETURNING i, j, k "
      + "    BULK COLLECT INTO ?, ?, ?; "
      + "END;")) {

    try {

        // Create the table and the auxiliary types
        s.execute(
            "CREATE TABLE x ("
          + "  i INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,"
          + "  j VARCHAR2(50),"
          + "  k DATE DEFAULT SYSDATE"
          + ")");
        s.execute("CREATE TYPE t_i AS TABLE OF NUMBER(38)");
        s.execute("CREATE TYPE t_j AS TABLE OF VARCHAR2(50)");
        s.execute("CREATE TYPE t_k AS TABLE OF DATE");

        // Bind input and output arrays
        c.setArray(1, ((OracleConnection) con).createARRAY(
            "T_J", new String[] { "a", "b", "c" })
        );
        c.registerOutParameter(2, Types.ARRAY, "T_I");
        c.registerOutParameter(3, Types.ARRAY, "T_J");
        c.registerOutParameter(4, Types.ARRAY, "T_K");

        // Execute, fetch, and display output arrays
        c.execute();
        Object[] i = (Object[]) c.getArray(2).getArray();
        Object[] j = (Object[]) c.getArray(3).getArray();
        Object[] k = (Object[]) c.getArray(4).getArray();

        System.out.println(Arrays.asList(i));
        System.out.println(Arrays.asList(j));
        System.out.println(Arrays.asList(k));
    }
    finally {
        try {
            s.execute("DROP TYPE t_i");
            s.execute("DROP TYPE t_j");
            s.execute("DROP TYPE t_k");
            s.execute("DROP TABLE x");
        }
        catch (SQLException ignore) {}
    }
}

这篇关于JDBC批处理INSERT,返回ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:52