有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何知道所有的记录都被插入其中一条记录,而不是在批插入中插入

我插入多个学生名单,代码如下

int count = 0;
for (Student st :StudentList) {
    ps.setString(1, l.getId());
    ps.setString(2, l.getName());
    ps.setString(3, l.getRollNo());
    ps.addBatch();
    if (++count % 100 == 0) {
        ps.executeBatch();
    }
}
ps.executeBatch();

我如何知道所有记录都已成功插入。如果不是,则插入/未插入列表中的哪个记录


共 (1) 个答案

  1. # 1 楼答案

    这是来自executeBatch的javadoc

    The int elements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch.

    The elements in the array returned by the method executeBatch may be one of the following:

    A number greater than or equal to zero indicates that the command was processed successfully and is an update count giving the number of rows in the database that were affected by the command's execution