表结构
CREATE TABLE `user` (
`u_id` int(11) NOT NULL AUTO_INCREMENT,
`u_name` varchar(255) NOT NULL,
`u_password` varchar(255) NOT NULL,
`u_realname` varchar(255) NOT NULL,
`u_email` varchar(255) DEFAULT NULL,
`u_r_id` int(10) NOT NULL,
`u_status` int(11) NOT NULL,
PRIMARY KEY (`u_id`),
KEY `ur_fk` (`u_r_id`),
CONSTRAINT `ur_fk` FOREIGN KEY (`u_r_id`) REFERENCES `role` (`role_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
DAO层
public int deleteMultiUser(String[] userids) {
this.getConn();
String sql = "delete from `user` where u_id in (?";
StringBuffer sqlbuf = new StringBuffer(sql);
try {
if (userids.length == 1) {
sqlbuf.append(")");
}else{
for (int i = 0; i < userids.length-1; i++) {
sqlbuf.append(",?");
}
sqlbuf.append(")");
}
System.out.println(sqlbuf.toString());
this.ps = conn.prepareStatement(sqlbuf.toString());
if (userids.length == 1) {
ps.setString(1, userids[0]);
}else{
for (int i = 1; i <= userids.length; i++) {
ps.setString(i, userids[i-1]);
}
}
return st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(conn, st, res);
}
return 0;
}
public int deleteMultiUser(String[] userids) {
this.getConn();
String sql = "delete from `user` where u_id = ?";
try {
ps = conn.prepareStatement(sql);
for (String userid : userids) {
ps.setString(1, userid);
ps.addBatch();
}
ps.executeBatch();
return 1;
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(conn, st, res);
}
return 0;
}