Results 1 to 2 of 2
  1. #1
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default deleting data from mysql database trough servlet / jsp

    Hi guys, i have my servlet that calls mysql database and it looks like this:

    adminController.java:
    Java Code:
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		String operation = request.getParameter("operation");
    		
    		
    		
    		if(operation == null)
    		{
    			
    			ArrayList<PageData> list;
    			ArrayList<PageData> subList;
    			Iterator<PageData> pageIterator;
    			list = dao.getPagesByParent("main");
    			pageIterator = list.iterator();
    			StringBuffer st = new StringBuffer();
    			st.append("<ul>");
    			while(pageIterator.hasNext())
    			{
    				PageData pageData = pageIterator.next();
    				st.append("<li>");
    				st.append(pageData.getTitle());
    				st.append(" <a href=\"adminController?operation=edit&id=");
    				st.append( pageData.getId());
    				st.append("\">Edit</a>");
    				st.append(" <a href=\"adminController?operation=delete&id=");
    				st.append( pageData.getId());
    				st.append("\">Delete</a>");
    				st.append("</a></li>");
    				
    				subList = dao.getPagesByParent(pageData.getId());
    				Iterator<PageData> subpageIterator = subList.iterator();
    				
    				st.append("<ul>");
    				while(subpageIterator.hasNext())
    				{
    					PageData subPageData = subpageIterator.next();
    					st.append("<li>");
    					st.append(subPageData.getTitle());
    					st.append(" <a href=\"adminController?operation=edit&id=");
    					st.append( subPageData.getId());
    					st.append("\">Edit</a>");
    					st.append(" <a href=\"adminController?operation=delete&id=");
    					st.append( subPageData.getId());
    					st.append("\">Delete</a>");
    					st.append("</a></li>");
    				}
    				st.append("<li><a href=\"adminController?operation=add&id=");
    				st.append( pageData.getId());
    				st.append("\">Add page</a></li>");
    				
    				st.append("</ul>");
    				st.append("</li>");
    			}
    			st.append("<li><a href=\"adminController?operation=add&id=main\">");
    			st.append("Add page</a></li>");
    			st.append("</ul>");
    			
    			RequestDispatcher dispatcher = request.getRequestDispatcher("/mainadmin.jsp");
    			request.setAttribute("menu", st.toString());
    			dispatcher.forward(request, response);
    		}
    		else if(operation.equals("add"))
    		{
    			RequestDispatcher dispatcher = request.getRequestDispatcher("/add.jsp");
    			dispatcher.forward(request, response);
    		}
    		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");
    		}
    	}
    and my deletePage method looks like this:
    Java Code:
    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();
    		}
    }
    my delete.jsp is as follows:
    Java Code:
    <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>
    my mainadmin.jsp:
    Java Code:
    <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 smth like "are you sure you want to delete?", submit button"delete". but what i've 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?
    Last edited by Galichka; 05-24-2012 at 12:35 AM.

  2. #2
    Galichka is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: deleting data from mysql database trough servlet / jsp

    oh, i got it, don't worry guys.

Similar Threads

  1. Replies: 1
    Last Post: 04-02-2012, 12:09 PM
  2. Replies: 2
    Last Post: 04-02-2012, 11:54 AM
  3. Dispaly data from mysql database into JSP.
    By kritijain in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-16-2011, 09:17 AM
  4. Sending data trough for to two JSP pages
    By katarina.the.great in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 09-04-2010, 06:30 PM
  5. Displaying data from mysql database.
    By Haresh gurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-09-2008, 03:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •