回答

收藏

从Java内部创建SQL批处理更新

技术问答 技术问答 205 人阅读 | 0 人回复 | 2023-09-14

我想更新mySql数据库中特定列上的每一行。目前,我在java.sql.PreparedStatement每行中使用一个,并在for循环中进行迭代。我想知道在Java编程方面是否还有其他选择可以减少这种时间和资源的消耗(类似于批量执行准备好的语句)。更新是从Java代码进行的,因为这是我从中获取值的地方。我也没有兴趣在服务器上制作存储过程,因为我没有这样做的权利。" M: `7 ]- C; E  c: P& L* z
                : s+ m! V! z) T) G5 f& \
解决方案:4 L4 h, E/ F1 s: B
               
7 t9 {8 g6 k4 c" t% y5 @& z+ q& Q& g* ^8 x2 i5 k' ?6 }/ G2 t
1 t# S9 e: A: [% X; E) ~$ n
                这里是一个示例的链接,该示例使用Java的Prepared语句执行批处理更新。我还提供了该站点的示例以供快速参考。
2 N8 j7 l( J, n# a5 t! U: V, \http://www.exampledepot.com/egs/java.sql/BatchUpdate.html
2 \! |; c( F  y; R0 @  a2 Etry {
' x3 O7 k1 T8 k" R' R  v    // Disable auto-commit# ]5 s) Y6 }- Y- w. B, I  c. a
    connection.setAutoCommit(false);9 Z: r" F$ X7 L: D
    // Create a prepared statement; a: ^/ }+ b, i" I0 a& k
    String sql = "INSERT INTO my_table VALUES(?)";  z( ~1 Q3 Y8 R  |; ~
    PreparedStatement pstmt = connection.prepareStatement(sql);
/ N7 w( H. G  V; X: F4 k    // Insert 10 rows of data
5 l: q$ v0 X/ m/ g) x9 c0 F' d" T9 p7 e+ ?    for (int i=0; i<10; i++) {
, c# r3 z1 J1 ?" ~        pstmt.setString(1, &quot;&quot;+i);
0 j/ T- o3 T- z# l! q        pstmt.addBatch();
- w" Z+ ~* M0 j8 R: z    }  @" S! W9 i5 c2 y6 W, T3 ^& p" c. r4 m, X
    // Execute the batch8 i! u, a; z6 Y3 B6 h
    int [] updateCounts = pstmt.executeBatch();
$ N& {* e. p9 k( n, l    // All statements were successfully executed.% l: B1 C0 S' K+ F. h. P
    // updateCounts contains one element for each batched statement.) }" R2 `+ [* Z; N
    // updateCounts<i> contains the number of rows affected by that statement.9 ~8 P9 x: q" }. s) [2 ]5 R
    processUpdateCounts(updateCounts);# p) l  \6 P+ m* v
    // Since there were no errors, commit' `% y9 ^# p6 I6 |! E% e
    connection.commit();
+ r2 k% ~/ ?# Q8 g% a} catch (BatchUpdateException e) {9 _0 a  y+ k) J1 }; \) E* Q
    // Not all of the statements were successfully executed
" G7 e- O0 c2 V+ c" J3 }    int[] updateCounts = e.getUpdateCounts();
$ a1 ^7 b/ ?# ]( C  z    // Some databases will continue to execute after one fails.
! i+ h3 S7 C  W: u8 @    // If so, updateCounts.length will equal the number of batched statements.$ o* I9 b" F0 q+ k# M3 e( j
    // If not, updateCounts.length will equal the number of successfully executed statements" D+ j+ Z4 O  H3 d" W
    processUpdateCounts(updateCounts);
2 Z8 b. x6 ^' R/ X( M- F9 e    // Either commit the successfully executed statements or rollback the entire batch
% _  Y; i/ G- ~    connection.rollback();6 |2 V) e. ^  C
} catch (SQLException e) {) I* z- K2 g6 E* E' {' R
}
! p2 [) H0 x" b/ S2 T9 wpublic static void processUpdateCounts(int[] updateCounts) {
' a7 v" @1 X$ b. [; a0 ]- @    for (int i=0; i<u>= 0) {+ G2 U- O" c! B8 s4 @
            // Successfully executed; the number represents number of affected rows' W- f- p  e% l3 C/ F' P. E) m
        } else if (updateCounts<i> == Statement.SUCCESS_NO_INFO) {
/ d5 z& }. g0 P; T3 y7 l            // Successfully executed; number of affected rows not available! `0 G) ?* T7 q; _3 N
        } else if (updateCounts<i> == Statement.EXECUTE_FAILED) {
% g' i' `4 [- n0 M5 j  L/ K$ q            // Failed to execute
0 O  f* J) x; G( \        }. n- q6 f* w) a% r3 _
    }# z2 q, T) M5 h" m9 ?3 A# }1 f
}
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则