从 Java 中创建 SQL 批量更新

发布于 2024-08-07 00:54:16 字数 193 浏览 2 评论 0原文

我想更新 mySql 数据库中特定列上的每一行。目前,我对每一行使用 java.sql.PreparedStatement 并在 for 循环中迭代。我想知道在 Java 编程方面是否还有其他替代方案可以减少时间和资源消耗(比如批量执行准备好的语句)。更新是通过 Java 代码进行的,因为这是我获取值的地方。我也对在服务器上创建存储过程不感兴趣,因为我没有这样做的权利。

I want to update every row on a specific column in a mySql database. Currently I am using a java.sql.PreparedStatement for each row and iterating in a for loop. I was wondering if there were any other alternatives in terms of Java programming to make this less time and resource consuming (something like executing the prepared statements in a batch). The updates are made from Java code because that is where I get the values from. I am also not interested in making stored procedures on the server as I do not have the rights for that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘴硬脾气大 2024-08-14 00:54:16

下面是一个示例的链接,该示例使用 Java 的准备语句来执行批量更新。我还提供了该网站的示例以供快速参考。

http://www.exampledepot.com/egs/java.sql/BatchUpdate.html

try {
    // Disable auto-commit
    connection.setAutoCommit(false);

    // Create a prepared statement
    String sql = "INSERT INTO my_table VALUES(?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    // Insert 10 rows of data
    for (int i=0; i<10; i++) {
        pstmt.setString(1, ""+i);
        pstmt.addBatch();
    }

    // Execute the batch
    int [] updateCounts = pstmt.executeBatch();

    // All statements were successfully executed.
    // updateCounts contains one element for each batched statement.
    // updateCounts[i] contains the number of rows affected by that statement.
    processUpdateCounts(updateCounts);

    // Since there were no errors, commit
    connection.commit();
} catch (BatchUpdateException e) {
    // Not all of the statements were successfully executed
    int[] updateCounts = e.getUpdateCounts();

    // Some databases will continue to execute after one fails.
    // If so, updateCounts.length will equal the number of batched statements.
    // If not, updateCounts.length will equal the number of successfully executed statements
    processUpdateCounts(updateCounts);

    // Either commit the successfully executed statements or rollback the entire batch
    connection.rollback();
} catch (SQLException e) {
}

public static void processUpdateCounts(int[] updateCounts) {
    for (int i=0; i<updateCounts.length; i++) {
        if (updateCounts[i] >= 0) {
            // Successfully executed; the number represents number of affected rows
        } else if (updateCounts[i] == Statement.SUCCESS_NO_INFO) {
            // Successfully executed; number of affected rows not available
        } else if (updateCounts[i] == Statement.EXECUTE_FAILED) {
            // Failed to execute
        }
    }
}

Here is a link to an example that uses Java's prepared statement to execute a batch update. I also included the sample from the site for quick reference.

http://www.exampledepot.com/egs/java.sql/BatchUpdate.html

try {
    // Disable auto-commit
    connection.setAutoCommit(false);

    // Create a prepared statement
    String sql = "INSERT INTO my_table VALUES(?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    // Insert 10 rows of data
    for (int i=0; i<10; i++) {
        pstmt.setString(1, ""+i);
        pstmt.addBatch();
    }

    // Execute the batch
    int [] updateCounts = pstmt.executeBatch();

    // All statements were successfully executed.
    // updateCounts contains one element for each batched statement.
    // updateCounts[i] contains the number of rows affected by that statement.
    processUpdateCounts(updateCounts);

    // Since there were no errors, commit
    connection.commit();
} catch (BatchUpdateException e) {
    // Not all of the statements were successfully executed
    int[] updateCounts = e.getUpdateCounts();

    // Some databases will continue to execute after one fails.
    // If so, updateCounts.length will equal the number of batched statements.
    // If not, updateCounts.length will equal the number of successfully executed statements
    processUpdateCounts(updateCounts);

    // Either commit the successfully executed statements or rollback the entire batch
    connection.rollback();
} catch (SQLException e) {
}

public static void processUpdateCounts(int[] updateCounts) {
    for (int i=0; i<updateCounts.length; i++) {
        if (updateCounts[i] >= 0) {
            // Successfully executed; the number represents number of affected rows
        } else if (updateCounts[i] == Statement.SUCCESS_NO_INFO) {
            // Successfully executed; number of affected rows not available
        } else if (updateCounts[i] == Statement.EXECUTE_FAILED) {
            // Failed to execute
        }
    }
}
↙厌世 2024-08-14 00:54:16

如果您使用 MySQL,我相信您的问题的简短答案是“否”。没有什么比这更快的了。

事实上,即使是准备好的陈述也不会给你带来任何好处。也许新版本改变了这一点,但我上次检查(几年前),MySQL 无论如何都只是将准备好的语句转换为常规语句。没有缓存任何内容。

If you're using MySQL, I believe the short answer to your question is "No". There's nothing you can do that will be any faster.

Indeed, even the prepared statement gains you nothing. Perhaps this is changed with newer versions, but last I checked (several years ago), MySQL just turns prepared statements into regular statements anyway. Nothing is cached.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文