<tfoot id='dyowy'></tfoot>
    <i id='dyowy'><tr id='dyowy'><dt id='dyowy'><q id='dyowy'><span id='dyowy'><b id='dyowy'><form id='dyowy'><ins id='dyowy'></ins><ul id='dyowy'></ul><sub id='dyowy'></sub></form><legend id='dyowy'></legend><bdo id='dyowy'><pre id='dyowy'><center id='dyowy'></center></pre></bdo></b><th id='dyowy'></th></span></q></dt></tr></i><div id='dyowy'><tfoot id='dyowy'></tfoot><dl id='dyowy'><fieldset id='dyowy'></fieldset></dl></div>
  1. <legend id='dyowy'><style id='dyowy'><dir id='dyowy'><q id='dyowy'></q></dir></style></legend>
  2. <small id='dyowy'></small><noframes id='dyowy'>

      <bdo id='dyowy'></bdo><ul id='dyowy'></ul>
    1. 从 Java 中创建 SQL 批量更新

      时间:2024-05-10

          <legend id='03Bq6'><style id='03Bq6'><dir id='03Bq6'><q id='03Bq6'></q></dir></style></legend>
        1. <small id='03Bq6'></small><noframes id='03Bq6'>

            <tbody id='03Bq6'></tbody>
            <bdo id='03Bq6'></bdo><ul id='03Bq6'></ul>
            <tfoot id='03Bq6'></tfoot>
            <i id='03Bq6'><tr id='03Bq6'><dt id='03Bq6'><q id='03Bq6'><span id='03Bq6'><b id='03Bq6'><form id='03Bq6'><ins id='03Bq6'></ins><ul id='03Bq6'></ul><sub id='03Bq6'></sub></form><legend id='03Bq6'></legend><bdo id='03Bq6'><pre id='03Bq6'><center id='03Bq6'></center></pre></bdo></b><th id='03Bq6'></th></span></q></dt></tr></i><div id='03Bq6'><tfoot id='03Bq6'></tfoot><dl id='03Bq6'><fieldset id='03Bq6'></fieldset></dl></div>
              1. 本文介绍了从 Java 中创建 SQL 批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想更新 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.

                推荐答案

                这是一个使用 Java 的预处理语句来执行批量更新的示例的链接.我还包括了网站上的示例以供快速参考.

                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
                        }
                    }
                }
                

                这篇关于从 Java 中创建 SQL 批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:获取 LDAP 对象的内部属性 下一篇:休眠 @DynamicUpdate(value=true) @SelectBeforeUpdate(value=true

                相关文章

                  <i id='NhEph'><tr id='NhEph'><dt id='NhEph'><q id='NhEph'><span id='NhEph'><b id='NhEph'><form id='NhEph'><ins id='NhEph'></ins><ul id='NhEph'></ul><sub id='NhEph'></sub></form><legend id='NhEph'></legend><bdo id='NhEph'><pre id='NhEph'><center id='NhEph'></center></pre></bdo></b><th id='NhEph'></th></span></q></dt></tr></i><div id='NhEph'><tfoot id='NhEph'></tfoot><dl id='NhEph'><fieldset id='NhEph'></fieldset></dl></div>
                1. <small id='NhEph'></small><noframes id='NhEph'>

                2. <tfoot id='NhEph'></tfoot>

                3. <legend id='NhEph'><style id='NhEph'><dir id='NhEph'><q id='NhEph'></q></dir></style></legend>
                  • <bdo id='NhEph'></bdo><ul id='NhEph'></ul>