JDBC批量更新

演示demo1

public int[] batchUpdate(Connection connection, String sql, Object[][] args) {
PreparedStatement pstmt = null;

int[] result;
try {
logger.debug("开始批量执行: [sql= " + sql + "]");
long beginTime = System.currentTimeMillis();
pstmt = connection.prepareStatement(sql);
for (int i = 0; i < args.length; ++i) {
Object[] curArgs = args[i];

for (int j = 1; j <= curArgs.length; ++j) {
pstmt.setObject(j, curArgs[j - 1]);
}

pstmt.addBatch();
}

result = pstmt.executeBatch();
if (logger.isDebugEnabled()) {
long time = System.currentTimeMillis() - beginTime;
logger.debug("批量执行结果:[" + Arrays.toString(result) + "],用时 [" + time + " millisecond]");
}

} catch (SQLException e) {
throw new JdbcException(e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

return result;
posted on 2020-08-12 14:41  山鹰羽眼  阅读(1671)  评论(0编辑  收藏  举报