package com.awu.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
-
Servlet 获得填写的表单数据
*/
public class DeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
request.setCharacterEncoding("UTF-8");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/mysql" ;//mysql为数据库的名称
String user = "root"; //数据库的登录名
String password = "123456"; //数据库的登录密码
Connection conn=null;
conn=DriverManager.getConnection(url, user, password);String flag=request.getParameter("flag"); String users=request.getParameter("id");//用数组接受前台传来的被选中的复选框的值 System.out.printf(users,flag); String[] Id=users.split(","); int m=0; if (Id.length>0) { for (int i = 0; i < Id.length; i++){ int id1=Integer.parseInt(Id[i]); String sql = "delete from test where id="+id1; //test为要查询的表 System.out.println(id1); m++; //根据前台选中的id值循环删除 //将删除落实到数据库删除 ??? //当删除次数等于选中复选框传过来的个数时,删除成功,否则失败,分别返回两种页面 } } if (m==Id.length) { request.setAttribute("delete_infos", "删除成功"); request.getRequestDispatcher("success.jsp").forward(request, response); }else{ request.setAttribute("delete_infos", "删除失败"); request.getRequestDispatcher("login.jsp").forward(request, response); } } catch (SQLException e) { System.out.println(e.getMessage()); } } }