qq_41843551
鱼在海上飞
采纳率82.5%
2018-05-02 02:38 阅读 2.1k
已采纳

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

5

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条回答 默认 最新

  • 已采纳
    congyeyan9940 笨笨的程序员啊 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());
        }
    }
    

    }

    点赞 1 评论 复制链接分享
  • qq_38873940 晓梦随风 2018-05-02 03:01
         //1 sql语句
        String sql = "delete from category where cid = ?";
        //2 获得预处理对象
        psmt = conn.prepareStatement(sql);
        //3设置实际参数
        psmt.setInt(1,id1);
        //4执行
        int r = psmt.executeUpdate();
    
    点赞 评论 复制链接分享
  • Zoey_C Zoey_C 2018-05-02 03:12

    直接删除 conn=executeUpdate(sql);
    计算执行成功条数 boolean boo = conn.execute(sql);
    if(boo==true){
    m++;
    }

    点赞 评论 复制链接分享

相关推荐