当我在Java中执行statement.executeBatch()方法时,它返回一个值为2的int数组

关于批处理更新的返回码,jdbc-spec有以下说法:

■0或更大

■声明。SUCCESS_NO_INFO

■-2

但是我每次都得到2。
记录已成功从数据库中删除,并且从两个满足查询条件的表中删除了25条记录。但是在sysout中,它将受影响的Records值显示为2

    Connection conn = null;
    Statement stmt = null;
    try {
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD);
        stmt = conn.createStatement();
        System.out.println("trying to execute query");

        stmt.addBatch("DELETE FROM TABLE1 WHERE CRTE_TSTP < TRUNC(SYSDATE) - 14");
        stmt.addBatch("DELETE FROM TABLE2 WHERE RECV_TSTP < TRUNC(SYSDATE) - 14");

        int affectedRecords[] = stmt.executeBatch();
        conn.commit();

        System.out.println("number of rows deleted  " + affectedRecords.length);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }


}

最佳答案

您得到的是数组的长度,而不是实际的int值。您是否尝试过计算返回数组的元素的删除总和。例如:

int affectedRecordsSum = affectedRecords[0] + affectedRecords[1];
System.out.println("number of rows deleted  " + affectedRecordsSum);

09-04 06:05