批量删除不了呢!求解求解!!!
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工管理信息</title>
<style type="text/css">
<!--
.STYLE2 {font-size: 36px}
-->
</style>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#fanhui").click(function(){
window.location.href= "../show.jsp";
});
$("#add").click(function(){
window.location.href= "add.jsp";
});
$("#update").click(function(){
window.location.href= "mod.jsp";
});
$("#del").click(function(){
window.location.href= "del.action";
});
$("#select").click(function(){
window.location.href= "sel.jsp";
});
$(document).ready(function() {
$("tr:odd").css({background: "#cccccc"});
$("tr:even").css({background: "#FFFFFF"});
});
$("#delAll").click(function(){
var str="";
var sel=document.getElementsByName("check"); //获取checkbox的值
for( var i=0;i<sel.length;i++)
if(sel[i].checked==true){
if(str == "")
str += sel[i].value;
else
str += ","+sel[i].value;
if(window.confirm("确定删除吗?")){
window.location.href= "delAll.action?id="+str;
}
}
alert(str);
if(str==""){
alert("请至少选择一条记录");
return false;
}
});
});
</script>
</head>
<body>
<center>
<table width="830" height="139" border="1">
<tr>
<td height="52" colspan="10"><div class="STYLE2">员工管理信息</div></td>
</tr>
<tr>
<td width="63"> </td>
<td width="64" height="36"><strong>员工ID</strong></td>
<td width="69"><strong>员工姓名</strong></td>
<td width="74"><strong>员工密码</strong></td>
<td width="74"><strong>员工性别</strong></td>
<td width="77"><strong>员工电话</strong></td>
<td width="74"><strong>员工地址</strong></td>
<td width="72"><strong>出生日期</strong></td>
<td width="74"><strong>员工简介</strong></td>
<td width="83"><div align="center"><strong>操作</strong></div></td>
</tr>
<s:iterator value="list" >
<tr>
<td align="right"><input type="checkbox" name="check" value=<s:property value="id"/> ></td>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="password"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="tel"/></td>
<td><s:property value="address"/></td>
<td><s:property value="birthday"/></td>
<td><s:property value="content"/></td>
<td><a href="#?yg.id=<s:property value="id"/>">查看</a>
<a href="del.action?yg.id=<s:property value="id"/>">删除</a>
<a href="mod.jsp?id=<s:property value="id"/>">修改</a></td>
</tr>
</s:iterator>
<tr>
<td height="38" colspan="10">
<div align="right">
<input type="button" name="fanhui" id="fanhui" value="返 回" />
<input type="button" name="add" id="add" value="增 加" />
<input type="button" name="delAll" id="delAll" value="删 除" />
<input type="button" name="select" id="select" value="查 询" />
</div>
</td>
</tr>
</table>
</center>
</body>
</html>
serv层
/**
* 批量删除
*/
public boolean dels(Connection conn,String[] str){
try {
conn = db.getConnection();
for(int i=0;i<str.length;i++){
String sql = "delete from Employee where id = ?";
String s = str[i];
pstat = conn.prepareStatement(sql);
pstat.setObject(1, s);
System.out.println("删除"+s);
System.out.println("批量删除成功"+sql);
pstat.executeUpdate();
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("删除失败");
return false;
}finally{
db.colse(pstat);
db.colse(conn);
}
}
action调用serv层方法
public String dels(){
String str ="" ;
String ids[] = str.split(",");
db.getConnection();
ygs.dels(conn, ids);
list = ygs.show(conn);
db.colseAll(conn, pstat, null);
return this.SUCCESS;
}
我在serv层上面做了个main方法
public static void main(String[] args) {
DButil db = new DButil();
Connection conn = null;
YuanGongServ ygs = new YuanGongServ();
conn = db.getConnection();
String[] str ={"1","2"};
System.out.println(str);
ygs.dels(conn, str);
}
运行是可以删除的,但是从页面选中复选框后点击删除按钮却不可以删除!大师们帮帮看啊!哪里出的问题!
[解决办法]
- Java code
action调用serv层方法public String dels(){String str ="" ;String ids[] = str.split(",");db.getConnection();ygs.dels(conn, ids);list = ygs.show(conn);db.colseAll(conn, pstat, null);return this.SUCCESS;}
[解决办法]
我没有仔细看你的代码,我只是想说,既然你用了struts2,可是在写js时候为什么调用.jsp,这样会不会
跳过action层呢????????????
[解决办法]
if(sel[i].checked==true){
if(str == "")
str += sel[i].value;
else
str += ","+sel[i].value;
}
if(window.confirm("确定删除吗?")){
window.location.href= "delAll.action?id="+str;
}
[解决办法]
String str ="" ;
String ids[] = str.split(",");
db.getConnection();
ygs.dels(conn, ids);
什么东西?str="";?????????????????????
[解决办法]
传的参数名也是id啊!
[解决办法]
这也问,看不懂。
[解决办法]
[解决办法]
第一不是成员变量。第二 名字也不匹配。