鱼在海上飞 2018-05-02 02:38 采纳率: 75%
浏览 2286
已采纳

servlet里面删除怎么写,谁能帮忙补充一下

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());
                                        } 
            }
    }                                                                                   
    
  • 写回答

3条回答 默认 最新

  • 笨笨的程序员啊 2018-05-02 03:19
    关注

    方法补充了一下,另外建议用预处理和事物去做。
    public class DeleteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    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;
            PreparedStatement pstm = 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;
            int deleteNum = 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值循环删除
                    pstm = conn.prepareStatement(sql);
                    int row = pstm.executeUpdate();
                    deleteNum = deleteNum + row;
                }
            }
            // 当删除次数等于选中复选框传过来的个数时,删除成功,否则失败,分别返回两种页面
            if (deleteNum == Id.length) { // m肯定是等于复选框的数量的
                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());
        }
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费