您的位置:首页 > 数据库

使用JDBC批量删除数据库记录条数

2014-06-17 10:05 99 查看
/**
* 使用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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: