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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM