连接表跟删除表的Class
public class Seleclass {
public Seleclass() {
}
public ArrayList list(){
ArrayList list=new ArrayList();
DBconn con=new DBconn();
String sql="select * from emp";
try {
Statement stmt = con.con().createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Bean1 bean = new Bean1();
bean.setEmployeeid(rs.getInt("employeeid"));
bean.setLastname(rs.getString("lastname"));
bean.setFirstname(rs.getString("firstname"));
list.add(bean);
System.out.println("添加成功");
}
} catch (SQLException ex) {
}
return list;
}
public void deleid(int id){
String sql="delete from emp where employeeid="+id+"";
DBconn con=new DBconn();
try {
Statement stmt = con.con().createStatement();
int i=stmt.executeUpdate(sql);
if(i>0){
System.out.println("删除成功!");
}
} catch (SQLException ex) {
}
}
一个名为BEAN1的JAVABEAN,里面存放数据库元素
用来删除的ACTION
public class Untitled1Action extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
try {
Untitled1ActionForm f = (Untitled1ActionForm) form;
Seleclass sel = new Seleclass();
String[] id = request.getParameterValues("check");
for (int i = 0; i < id.length; i++) {
sel.deleid(Integer.parseInt(id[i]));
System.out.println("删除成功");
}
return mapping.findForward("index");
} catch (NumberFormatException ex) {
return mapping.findForward("error");
}
}
}
JSP页面
<table border="1" bgcolor="2">
<tr><td>编号</td><td>姓</td><td>名</td><td>操作</td>
</tr>
<% Seleclass sel=new Seleclass();
ArrayList list=sel.list();
for(int i=0;i<list.size();i++){
Bean1 user=(Bean1)list.get(i);%>
<tr><td>
<input type="checkbox" name="check" value="<%=user.getEmployeeid() %>"><%=user.getEmployeeid() %>
</td><td><%=user.getLastname() %>
</td><td><%=user.getFirstname() %>
</td><td><a href="update.jsp?employeeid=<%=user.getEmployeeid()%>">
更新</a></td>
</tr>
<%} %>
<tr><td>
<input type="submit" name="Submit" value="删除"></td><td>
<input type="reset" value="Reset"></td></tr></table>