SQLite 中的 JavaPreparedStatement.executeBatch() 不起作用

问题描述 投票:0回答:2

我有以下方法。注释的方法 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;
    }
java sqlite prepared-statement
2个回答
9
投票

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

您错误地使用了

addBatch()
方法。在你的代码中你正在做:

for (CpuData o : mData.getCpu()) {
    // 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(...)
一次。你应该做类似下面的事情。您必须更改插入语句以具有
?
参数:

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()
但这是 without 永远不会替换您正在执行的
prep
准备好的语句。

如果您尝试在同一批处理中连续执行一系列不同插入语句,那么您应该考虑关闭自动提交,执行语句,调用提交,然后重新打开自动提交。比如:

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

-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 查询。

© www.soinside.com 2019 - 2024. All rights reserved.