/**
* 使用JDBC批量删除数据
*/
public void deleteBatch() {
Connection con = null;
PreparedStatement stmt = null;
try {
// 获取数据库连接
con = getDBConnection();
con.setAutoCommit(false);
// 带有占位符的sql
String deleteSql = "delete from userInfo where id =?";
stmt = con.prepareStatement(deleteSql);
for (int i = 200; i < 999; i++) {
// 1是占位符的位置,i是取代占位符的值
stmt.setInt(1, i);
// 添加到批量
stmt.addBatch();
}
// 返回批量执行的条数
int[] result = stmt.executeBatch();
con.commit();
System.out.println(result.length);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeResources(null, stmt, con);
}
}
使用JDBC批量删除数据库记录条数
最新推荐文章于 2023-01-06 15:27:27 发布