[英]How to successfully remove a tag from a book in Java?
我正在从事 PubHub 100 项目。 我面临的一个问题是,当我按下按钮从书中删除标签时,网页会返回异常。 在这个项目的过程中,我使用了Java Development Kit,Eclipse编写和运行java程序,Wildfly作为服务器连接PubHub网站,PostgreSQL访问包含书籍的数据库。 下面是来自 Tag DAO 和 Tag DAO 实现的代码以及用于从书中删除标签的 servlet 和 JSP。
///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>
我可以看到的一件事是,您有一个带有参数的 SQL 查询:
DELETE From book_tags WHERE tags=?
但是您要设置两个参数的值:
stmt.setString(1, tag.getIsbn13());
stmt.setString(2, tag.getTag());
我认为您应该在查询中使用 ISBN13。
更新:
查询应该是:
从 book_tags 中删除 isbn_13=? AND 标签=?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.