全选框代码

1.<script type="text/javascript">
function checkDelete() {
var ids = document.getElementsByName("id");
if(document.formDelete.selectAll.checked) {
for(var i=0; i<ids.length; i++) {
ids[i].checked="checked";
}
} else {
for(var i=0; i<ids.length; i++) {
ids[i].checked="";
}
}
}
</script>

2.复选框全选按钮
对应商品前面的ID号:
<input type="checkbox" name="id" value="<%=p.getId()%>"/>

<input name="selectAll" type="checkbox" onClick="checkDelete()"/>
<input type="submit" value="Delete"/>

3.调用类中的delete方法:
public void delete(String[] idArray) {
String conditionStr = " where id in (";
for (int i = 0; i < idArray.length; i++) {
conditionStr += idArray[i];
conditionStr += ",";
}
conditionStr = conditionStr.replaceAll(".$", ")");//把最后那个ID号后面的逗号替换成")"
//conditionStr = conditionStr.substring(0, conditionStr.length() - 1) + ")";
System.out.println(conditionStr);
dao.delete(conditionStr);//调用具体实现类
}

4.具体实现类的delete方法
public void delete(String conditionStr) {
Connection conn = null;
Statement stmt = null;
String sql;
try {
conn = DB.getConn();
sql = "delete from product " + conditionStr;
stmt = DB.getStatement(conn);
DB.executeUpdate(stmt, sql);
} finally {
DB.close(stmt);
DB.close(conn);
}
}

你可能感兴趣的:(JavaScript,DAO,sql)