Problem Delete MySQL data from a list in Servlet

0

I have a problem deleting a record. This is my class Queries where I delete a user.

public static int Borrar(int id){
    int estado=0;
    try {
        Connection con = Consultas.getConnection();
        String sql = "call eliminar(?)";
        PreparedStatement ps = con.prepareStatement(sql);
        //e.getId cambia a id
        ps.setInt(1, id);

        estado = ps.executeUpdate();
        System.out.println("Lo borro");
        con.close();
    } catch (Exception d) {
        System.out.println("No lo borro");
        System.out.println(d.getMessage());
        System.out.println(d.getStackTrace());
    }
    return estado;
}

This is the servlet where the users appear and the option to delete them.

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<link rel=\"stylesheet\" href=\"CSS/TABLES.css\">");
        out.println("<br><h1> ¡Hola Administrador! </h1>");

        List<Datos> lista = Consultas.getAllEmpleados();

        out.println("<table align='center'>");
        out.println("<tr>"
                + "<th> ID </th>"
                + "<th> Nombre </th>"
                + "<th> Correo </th>"
                + "<th> Usuario </th>"
                + "<th> Contraseña </th>"
                + "<th>  </th>"
                + "<th>  </th>"
                + "</tr>");
        for(Datos e:lista){
            out.println("<tr>"
                    + "<td>"+e.getId()+"</td>"
                    + "<td>"+e.getNombre()+"</td>"
                    + "<td>"+e.getCorreo()+"</td>"
                    + "<td>"+e.getUsuario()+"</td>"
                    + "<td>"+e.getContrasena()+"</td>"
                    + "<td><a href='EditarServlet? id="+e.getId()+"'>Editar</a></td>"
                    + "<td><a href='Borrar?  id1="+e.getId()+" '>Borrar</a></td>"
                    + "</tr>");
        }
        out.println("</table>");
        out.close();
    }
}

And this is the Servlet to erase, I think the problem is in redirecting the list to this servlet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        String eseid=request.getParameter("id1");
        int id = Integer.parseInt(eseid);
        System.out.println(id);
        Consultas.Borrar(id);
        response.sendRedirect("listausuarios");
        out.println("</html>");
    }
}
    
asked by Estefania 06.05.2017 в 01:35
source

1 answer

0

The exception mentioned in the comments indicates that request.getParameter("id1") returns null and then the exception occurs when trying to interpret that id as integer in int id = Integer.parseInt(eseid) . Then it seems that the parameter id1 is not specified in the request.

Reviewing the servlet code that generates the link, I notice that there are spaces between the question mark ? and the names of the parameters in the links that are generated:

+ "<td><a href='EditarServlet? id="+e.getId()+"'>Editar</a></td>"
+ "<td><a href='Borrar?  id1="+e.getId()+" '>Borrar</a></td>"

So I daresay that is the problem, try to change the code to:

+ "<td><a href='EditarServlet?id="+e.getId()+"'>Editar</a></td>"
+ "<td><a href='Borrar?id1="+e.getId()+" '>Borrar</a></td>"
    
answered by 06.05.2017 в 02:30