简体   繁体   中英

How to successfully remove a tag from a book in Java?

I'm working on PubHub 100 project. One issue I'm facing is that when I press the button to remove a tag from a book, the webpage returns an exception. During the course of this project, I have used Java Development Kit, Eclipse to write and run java programs, Wildfly as a server to connect the PubHub website, and PostgreSQL to access the database containing the books. Here is the code from the Tag DAO and the Tag DAO Implementation along with the servlet and JSP to remove the tag from a book.

    ///Code from the Tag DAO

    package examples.pubhub.dao;

    import java.util.List;

    import examples.pubhub.model.Book;
    import examples.pubhub.model.Tag;

    public interface TagDAO {
    
        public List<Tag> getAllTags();
        public List<Tag> getAllBooks();
        public List<Tag> getTagsByBook(String isbn13);
        public List<Book> getBooksByTag(String booktag);
        public Tag getBooksByISBN(String isbn);
    
        public boolean AddTag(Tag tag);
        public boolean RemoveTag(Tag tag);
    }

    ///Code with SQL statement to remove tag from "TagDAOImpl that Implements TagDAO"
    @Override
        public boolean RemoveTag(Tag tag) {
        
        try {
            connection=DAOUtilities.getConnection();
            String Sql = "DELETE From book_tags WHERE tags=?";
            stmt = connection.prepareStatement(Sql);
            
            stmt.setString(1, tag.getIsbn13());
            stmt.setString(2, tag.getTag());
            
            if(stmt.executeUpdate() != 0)
                return true;
            else
                return false;
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
                
            } finally {
                closeResources();
            }
    }

    ///Servlet to remove the tag from the book. 

    package examples.pubhub.servlets;

    import java.io.IOException;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import examples.pubhub.model.Tag;
    import examples.pubhub.dao.TagDAO;
    import examples.pubhub.utilities.DAOUtilities;



    @WebServlet("/RemoveTag")
    public class RemoveTagServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
       
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
          ServletException, IOException {
        boolean isSuccess = false;
        
        
        String isbn13 = request.getParameter("isbn13");
        Tag tag = new Tag();
        
        tag.setIsbn13(isbn13);
        tag.setTag(request.getParameter("book_tags"));
        
        TagDAO dao = DAOUtilities.getTagDAO();
        isSuccess = dao.RemoveTag(tag);
        
        
        
        
        if(isSuccess) {
            request.getSession().setAttribute("message", "Book Tag Successfully Removed.");
            request.getSession().setAttribute("messageClass", "alert-success");
            response.sendRedirect("ViewBookDetails?isbn13=" + isbn13);
        } else {
            request.getSession().setAttribute("message", "There was a problem updating this tag");
            request.getSession().setAttribute("messageClass", "alert-danger");
            request.getRequestDispatcher("bookDetails.jsp").forward(request, response);
        }
    }
}


    //JSP Code to create a section where the user clicks to remove the tag from the book details.
    <section>
     <div class="container">
       <h1><small>Tags</small></h1>
       <table
          class="table table-striped table-hover table-responsive pubhub-datatable">
           <thead>
             <tr>
               <th>Tag</th>
               <td></td>
            </tr>
           </thead>
          
           <tbody>
             <c:forEach var="tag" items="${tags}">
               <tr>
                  <td><c:out value="${tag.tag}"/></td>
                  <td>
                    <form method="post" action="RemoveTag">
                      <input type="hidden" value="${tag.isbn13}" name="isbn13"/>
                      <input type="hidden" value="${tag.tag}" name="tag"/>
                      <button class="btn btn-danger popover-bookpub-removetag" 
                          type="submit">Remove Tag</button>
                    </form>
                  </td>
                </tr>
              </c:forEach>
            </tbody>
         </table>
       
       </div>
      </section>

One thing I can see, is that you have an SQL query with a parameter:

DELETE From book_tags WHERE tags=?

But you're setting the value of two parameters:

stmt.setString(1, tag.getIsbn13());
stmt.setString(2, tag.getTag());

I think you should use the ISBN13 in your query.

UPDATE:

The query should be:

DELETE FROM book_tags WHERE isbn_13=? AND tags=?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM