Gipsy
Gipsy

Reputation: 265

deleting data from mysql database trough servlet / jsp

I have a servlet that calls MySQL database.

The deletePage method looks like this:

public void  deletePage(PageData delete) {
    String firstQuery ="Delete FROM pages Where ID= '"+delete.getId()+"';";

    try {
        statement = connection.createStatement();
        statement.executeUpdate(firstQuery);
        System.out.println("Expense is deleting");

    } catch (SQLException e) {
        System.out.println("Expense isn't deleting - SQLException");
        e.printStackTrace();
    }
}

The delete.jsp is as follows:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add pages</title>
    </head>
    <body>
        <form action="adminController">
            <p>
                <input type="hidden" name="operation" value="deletepage" />
                <input type="hidden" name="parentid" value="<%= request.getParameter("id") %>" />
                Enter ID of page you want to delete:<input name="id"><br>

                <input type="submit" />
            </p>
        </form>
    </body>
</html>

The mainadmin.jsp:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%= request.getAttribute("menu") %>
    </body>
</html>

if you run this code you'll see that my delete buttons stands after each page name. what i want to do is by pressing the delete button near the page to delete this page, there should be something like "are you sure you want to delete?", submit button"delete". but what i have managed to do looks like this(as in my code): whenever delete button is pressed, you have to enter id of page you want to delete, so no matter what delete button i press, it requests for id. but i just want to delete it right away. can anyone help?

Upvotes: 1

Views: 9373

Answers (1)

Panos Bariamis
Panos Bariamis

Reputation: 4653

in adminController.java you have

else if (operation.equals("delete")) {
    RequestDispatcher dispatcher = request.getRequestDispatcher("/delete.jsp");
    dispatcher.forward(request, response);

} else if (operation.equals("deletepage")) {
    PageData pageData = new PageData();
    pageData.setId(request.getParameter("id"));
    dao.deletePage(pageData);
    response.sendRedirect("adminController");
}

change it to

else if (operation.equals("delete")) {
    PageData pageData = new PageData();
    pageData.setId(request.getParameter("id"));
    dao.deletePage(pageData);
    response.sendRedirect("adminController");
}

you do not need the second operation 'deletepage', so you do not need delete.jsp

Upvotes: 2

Related Questions