gpt4 book ai didi

java - 准备好的语句未更新记录

转载 作者:行者123 更新时间:2023-12-01 11:29:21 27 4
gpt4 key购买 nike

这是 View :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Books </title>
</h:head>
<h:body>

<h:link value="Add Books" outcome="addBook.xhtml"></h:link>
<table border="1">
<tr>
<td><b>ID</b> </td>
<td><b>Title</b> </td>
<td><b>Year</b> </td>
<td><b>ISBN</b> </td>
<td><b>Total</b> </td>
<td><b>Remaining</b></td>

</tr>
<ui:repeat value="#{booksController.fetch_book_data}" var="r">

<tr>
<h:form>
<td><h:inputText value="#{r.id}"></h:inputText></td>
<td><h:inputText value="#{r.title}"></h:inputText> </td>
<td><h:inputText value="#{r.year}"></h:inputText> </td>
<td><h:inputText value="#{r.isbn}"></h:inputText> </td>
<td><h:inputText value="#{r.total}"></h:inputText> </td>
<td><h:inputText value="#{r.remaining}"></h:inputText></td>
<td><h:commandButton value="update record" action="#{book.setUpdateBook_data()}"/></td>
</h:form>
</tr>

</ui:repeat>
</table>

</h:body>
</html>

View 将数据提交给 setUpdateBook_data() 这是其定义:

public String setUpdateBook_data() throws SQLException {
this.setRemaining(this.getTotal());
Statement stmt = con.createStatement();

String query = "UPDATE books SET title=? , year123=? , isbn=? , total=? , remaining=? where id=?";
PreparedStatement preparedStatement = con.prepareStatement(query);

preparedStatement.setString(1, this.getTitle());
preparedStatement.setString(2, this.getYear());
preparedStatement.setString(3, this.getIsbn());
preparedStatement.setInt(4, this.getTotal());
preparedStatement.setInt(5, this.getRemaining());
preparedStatement.setInt(6, this.getId());
preparedStatement.executeUpdate();

preparedStatement.close();
stmt.close();

return "success";

}

提交数据后,应用程序显示没有错误,但记录未更新。我错过了什么?

我已经直接从 Netbeans 测试了以下查询,它在那里工作,但在这里不起作用。

UPDATE books SET title='qqq' , year123='3009' , isbn='123456asdfgh' , total=10 , remaining=5 where id=5

最佳答案

如果没有错误,但执行 executeUpdate 后没有数据,您可能没有提交事务,因为禁用了 autoCommit 选项。

尝试执行提交,如下所示:

preparedStatement.executeUpdate();
con.commit();

preparedStatement.close();

刚刚注意到您正在尝试从上下文之外访问某些内容:

请参阅此处的解释,这应该解释了您需要了解的所有内容: https://stackoverflow.com/a/3791736/4956256

关于java - 准备好的语句未更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30548074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com