java - 如何在 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=?和标签=?
推荐阅读
- typescript - ANTLR4 TypeScript 目标找不到模块或属性?
- php - file_get_contents:如何获取 json(我的错误代码不起作用)?
- python - 为时间序列预测准备数据
- ruby-on-rails - Rails 6 PaperTrail 仅从一个模型中提取日志
- keyboard-shortcuts - 使用键盘选择块
- r - 在 R 中导出数据并更改行颜色
- c# - .Net Winforms图片框自定义拖拽很抽搐
- just-audio - 音频播放器在暂停一分钟后变得无响应
- javascript - 从 Uint8Array 转换为 ArrayBuffer 前加 32 个字节?
- c# - 如何将重复的 XML 元素序列化为数组?