SQLite中的Java PreparedStatement.executeBatch()不起作用

时间:2012-05-18 21:26:39

标签: java sqlite prepared-statement

我有以下方法。注释方法saveOrUpdateToDatabase执行完全正常,但我想使用executeBatch。我错过了什么?接收executeBatch()结果的int [] r总是空的......

public boolean saveOrUpdate(MonitoredData mData) {
        try {
            PreparedStatement prep;
            String timeID = this.getTimeLastRowID();

            for (CpuData o : mData.getCpu()) {
                prep = this.conn.prepareStatement(String.format(
                        INSERT_CPU_USAGE, this.nodeID, timeID, o.toString()));
                prep.addBatch();
                // saveOrUpdateToDatabase(String.format(INSERT_CPU_USAGE,
                // this.nodeID, timeID, o.toString()));
            }
            for (DiskData o : mData.getDisk()) {
                prep = this.conn.prepareStatement(String.format(
                        INSERT_DISK_USAGE, this.nodeID, timeID, o.toString()));
                prep.addBatch();
                // saveOrUpdateToDatabase(String.format(INSERT_DISK_USAGE,
                // this.nodeID, timeID, o.toString()));

            }
            for (NetworkData o : mData.getNet()) {
                prep = this.conn.prepareStatement(String
                        .format(INSERT_NETWORK_USAGE, this.nodeID, timeID, o
                                .toString()));
                prep.addBatch();
                // saveOrUpdateToDatabase(String.format(INSERT_NETWORK_USAGE,
                // this.nodeID, timeID, o.toString()));

            }

            prep = this.conn.prepareStatement(String.format(
                    INSERT_MEMORY_USAGE, this.nodeID, timeID, mData.getMem()
                            .toString()));
            // saveOrUpdateToDatabase(String.format(INSERT_MEMORY_USAGE,
            // this.nodeID, timeID, mData.getMem().toString()));

            conn.setAutoCommit(false);
            int[] r = prep.executeBatch();
            conn.setAutoCommit(true);

            return true;

        } catch (SQLException ex) {
            Logger.getLogger(HistoricalDatabase.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        return false;
    }

2 个答案:

答案 0 :(得分:6)

  

我错过了什么?接收executeBatch()结果的int [] r总是空的......

您错误地使用了addBatch()方法。在您的代码中:

for (CpuData o : mData.getCpu()) {
    // this is wrong, you can not prepare a new query each time
    prep = this.conn.prepareStatement(INSERT_CPU_USAGE);
    prep.setObject(1, this.nodeID);
    prep.addBatch();
}

每次都替换准备好的查询。您应该每批只调用一次prepareStatement(...)。你应该做类似以下的事情。您必须将insert语句更改为?个参数:

PreparedStatement prep = conn.prepareStatement(INSERT_CPU_USAGE);
for (CpuData o : mData.getCpu()) {
    prep.setObject(1, this.nodeID);
    prep.setObject(2, timeID);
    prep.setObject(3, o);
    prep.addBatch();
}
prep.executeBatch();

请注意,只使用了一个prepareStatement()来电。其中包含?个SQL参数实体,然后在循环中使用prep.setString(1, ...)进行分配。当批处理准备好执行时,您可以调用prep.executeBatch(),但这是,而不会替换您正在执行的prep预处理语句。

如果您尝试在同一批次中连续执行一系列不同插入语句,那么您应该考虑关闭自动提交,执行语句,调用提交,然后转重新启动自动提交。类似的东西:

conn.setAutoCommit(false);
// statements prepared and executed here
// maybe no need for batch operations
...
conn.commit();
conn.setAutoCommit(true);

答案 1 :(得分:-1)

除了我写的评论之外,我还是冒险给出答案。如果您尝试这样的事情该怎么办:

public boolean saveOrUpdate(MonitoredData mData) {
    try {
        PreparedStatement prep;
        String timeID = this.getTimeLastRowID();
        conn.setAutoCommit(false);
        for (CpuData o : mData.getCpu()) {
            prep = this.conn.prepareStatement(INSERT_CPU_USAGE);
            prep.setObject(1, this.nodeID);
            prep.setObject(2, timeID);
            prep.setObject(3, o);
            prep.addBatch();
            // saveOrUpdateToDatabase(String.format(INSERT_CPU_USAGE,
            // this.nodeID, timeID, o.toString()));
        }
        prep.executeBatch();
        for (DiskData o : mData.getDisk()) {
            prep = this.conn.prepareStatement(INSERT_DISK_USAGE);
            prep.setObject(1, this.nodeID);
            prep.setObject(2, timeID);
            prep.setObject(3, o);
            prep.addBatch();
            // saveOrUpdateToDatabase(String.format(INSERT_DISK_USAGE,
            // this.nodeID, timeID, o.toString()));
        }
        prep.executeBatch();
        for (NetworkData o : mData.getNet()) {
            prep = this.conn.prepareStatement(INSERT_NETWORK_USAGE);
            prep.setObject(1, this.nodeID);
            prep.setObject(2, timeID);
            prep.setObject(3, o);
            prep.addBatch();
            // saveOrUpdateToDatabase(String.format(INSERT_NETWORK_USAGE,
            // this.nodeID, timeID, o.toString()));

        }
        prep.executeBatch();
        prep = this.conn.prepareStatement(INSERT_MEMORY_USAGE);
        prep.setObject(1, this.nodeID);
        prep.setObject(2, timeID);
        prep.setObject(3, o);
        prep.executeUpdate();
        // saveOrUpdateToDatabase(String.format(INSERT_MEMORY_USAGE,
        // this.nodeID, timeID, mData.getMem().toString()));

        conn.setAutoCommit(true);
        return true;

    } catch (SQLException ex) {
        Logger.getLogger(HistoricalDatabase.class.getName()).log(
                Level.SEVERE, null, ex);
    }
    return false;
}

您可能需要修改常量中定义的SQL查询。