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