如何在 Java 中成功地从书中删除标签?
Posted
技术标签:
【中文标题】如何在 Java 中成功地从书中删除标签?【英文标题】:How to successfully remove a tag from a book in Java? 【发布时间】:2021-09-12 20:13:45 【问题描述】:我正在从事 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>
【问题讨论】:
你遇到了什么异常? 我在网页上遇到异常,并以红色短暂显示“出现问题”语句以及“您的代码引发异常”语句。 【参考方案1】:我可以看到,你有一个带参数的 SQL 查询:
DELETE From book_tags WHERE tags=?
但是你设置了两个参数的值:
stmt.setString(1, tag.getIsbn13());
stmt.setString(2, tag.getTag());
我认为您应该在查询中使用 ISBN13。
更新:
查询应该是:
从 book_tags 中删除 isbn_13=? AND 标签=?
【讨论】:
您好 Maurice,感谢您的回复,我尝试在查询中使用 ISBN 13,但它并没有消除网页错误。我写了这样的语句 String sql = "DELETE FROM book_tags WHERE isbn_13=?"; stmt.setString(1, tag.getIsbn13()); stmt.setString(2, tag.getTag()); 同样的问题:您的 SQL 查询中有一个参数,但您传递了两个。您需要在查询中指定两者。查看我的更新以上是关于如何在 Java 中成功地从书中删除标签?的主要内容,如果未能解决你的问题,请参考以下文章
日本菲尔兹奖得主广中平祐:不要急于从书中寻找答案,要尝试独立思考