视图如下:
<?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