
JAVA批量删除问题
为什么通过参数传递进去不行呢?StringBufferstrPrame=newStringBuffer();for(inti=0;i<userCodeList.lengt...
为什么通过参数传递进去不行呢?
StringBuffer strPrame = new StringBuffer();
for (int i = 0; i < userCodeList.length; i++) {
strPrame.append("'");
strPrame.append(userCodeList[i]);
strPrame.append("'");
strPrame.append(",");
}
String sql = "DELETE FROM Users WHERE userCode IN (?)";
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(sql);
String Prames = strPrame.substring(0, strPrame.length() - 1);
pstmt.setString(1, Prames);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
用拼接字符串倒是可以,用穿参删除失败,是哪点错了? 展开
StringBuffer strPrame = new StringBuffer();
for (int i = 0; i < userCodeList.length; i++) {
strPrame.append("'");
strPrame.append(userCodeList[i]);
strPrame.append("'");
strPrame.append(",");
}
String sql = "DELETE FROM Users WHERE userCode IN (?)";
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(sql);
String Prames = strPrame.substring(0, strPrame.length() - 1);
pstmt.setString(1, Prames);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
用拼接字符串倒是可以,用穿参删除失败,是哪点错了? 展开
展开全部
public boolean delUser(int[] id) {
boolean flag = false;
String sql = "delete from userInfo where userId=?";
conn = base.getConnection();
PreparedStatement pst = null;
try {
pst = conn.prepareStatement(sql);
for (int i = 0; i < id.length; i++) {
pst.setInt(1, id[i]);
// 使用批处理
pst.addBatch();
}
// 执行批处理
int[] result = pst.executeBatch();
if (result[0] > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
base.free(conn, pst, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
只能这样来进行批量删除 好好的看一下
boolean flag = false;
String sql = "delete from userInfo where userId=?";
conn = base.getConnection();
PreparedStatement pst = null;
try {
pst = conn.prepareStatement(sql);
for (int i = 0; i < id.length; i++) {
pst.setInt(1, id[i]);
// 使用批处理
pst.addBatch();
}
// 执行批处理
int[] result = pst.executeBatch();
if (result[0] > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
base.free(conn, pst, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
只能这样来进行批量删除 好好的看一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询