boolean isSupportBatchUpdates(Connection conn)
DatabaseMetaData dbm = con.getMetaData();
if(dbm.supportBatchUpdates())
return true;
//notice: catch SQLException,AbstractMethodError
return false;
int[] batchUpdate(String[] sql)
//make sure sql is not null!!!
int res = new int[sql.length];
if(isSupportBatchUpdates(conn)){
for(int i = 0;i<sql.length;i++)
stmt.addBatch(sql[i]);
res = stmt.executeBatch();
} else {
for(int i = 0 ;i<sql.length;i++){
if(!stmt.execute(sql[i]))
res[i] = stmt.getUpdateCount();
else throw new ....
}
}
return res;
批量更新支持检查与执行
本文介绍了一种检查数据库是否支持批量更新的方法,并提供了在支持与不支持批量更新的情况下执行SQL批处理的具体实现。通过booleanisSupportBatchUpdates方法判断数据库元数据是否支持批量更新功能,若支持,则使用批处理方式执行SQL;反之则逐条执行并获取更新计数。
321

被折叠的 条评论
为什么被折叠?



