2015-05-30 154 views
0

這裏不更新是視圖:記錄預處理語句

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

如果沒有錯誤,但在執行的executeUpdate你可能不承諾,因爲禁用自動提交選項的交易,後無數據。

嘗試執行提交,如下所示:

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

preparedStatement.close(); 

只注意到你想從出上下文的訪問某些內容:

這裏看到這個解釋,這應該解釋everyting你需要知道: https://stackoverflow.com/a/3791736/4956256

+0

試過了,仍然沒有工作。 – user4913383

+0

您是否調試過您的應用程序,並檢查傳遞給查詢的所有值是否是您期望它們的值? –

+0

我只確保我使用這種方法發送正確的值http://superuser.com/questions/395919/where-is-the-post-tab-in-chrome-developer-tools-network但是我不'不知道如何在NetBeans中調試JSF應用程序。當我標記了一些斷點時,控制並沒有停在那裏。 – user4913383