记录未通过准备好的声明更新

时间:2015-05-30 15:42:18

标签: java sql jsf prepared-statement derby

以下是观点:

<?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

1 个答案:

答案 0 :(得分:1)

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

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

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

preparedStatement.close();

注意到你正试图从上下文中访问某些内容:

在这里看到这个解释,这应该解释你需要知道的每一个: https://stackoverflow.com/a/3791736/4956256