浏览 1362 次
锁定老帖子 主题:在数据库中执行一个批处理SQL语句(转)
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-05-08
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 } } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |