2013-04-16 19 views
0

我有一個單獨的類此方法,'ID' 參數進入的PreparedStatement爲空

public void updateBook(String bookName, String bookAuthor, 
      String bookGenres, String bookDesc, Date bookDueDate, 
      String bookStatus, String fullname, int id) { 
     try { 
      Class.forName(driverName).newInstance(); 
     } catch (InstantiationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      Connection connection = DriverManager.getConnection(dbURL, 
        username, password); 

      String updateQuery = "UPDATE bookTrackerSystem SET bookname=?, bookAuthor=?, bookGenres=?, bookDesc=?, bookDueDate=?, fullname=? WHERE id=?"; 
      PreparedStatement update = connection.prepareStatement(updateQuery); 

      update.setString(1, bookName); 
      update.setString(2, bookAuthor); 
      update.setString(3, bookGenres); 
      update.setString(4, bookDesc); 
      update.setDate(5, bookDueDate); 
      update.setString(6, bookStatus); 
      update.setString(7, fullname); 

      update.executeUpdate(); 
      System.out.println(update.toString()); 
      update.close(); 
      connection.close(); 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

    } 

這JSP頁面,

</head> 
<% 
    request.getParameter("id"); 
    session.setAttribute("id", request.getParameter("id")); 
%> 
<body> 
    <div id="formDiv"> 

     <form action="UpdateBook" method="POST"> 
      <table> 
       <tr> 
        <td>Book Name:</td> 
        <td><input type="text" name="bookName"></td> 
       </tr> 
       <tr> 
        <td>Book Author:</td> 
        <td><input type="text" name="bookAuthor"></td> 
       </tr> 
       <tr> 
        <td>Book Genre:</td> 
        <td><input type="text" name="bookGenre"></td> 
       </tr> 
       <tr> 
        <td>Book Description:</td> 
        <td><input type="text" name="bookDesc"></td> 
       </tr> 
       <tr> 
        <td>Book Due Date :</td> 
        <td><input type="text" name="bookDueDate"></td> 
       </tr> 
       <tr> 
        <td>Book Status:</td> 
        <td><input type="text" name="bookStatus"></td> 
       </tr> 

       <tr> 
        <td colspan="2"><input type="submit" name="submit" 
         value="Update Book"></td> 
       </tr> 


      </table> 
     </form> 
    </div> 

</body> 

按鈕被點擊

後,該Servlet處理
protected void doPost(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 

     HttpSession session = request.getSession(true); 
     String id_str = session.getAttribute("id").toString(); 

     BookTrackerBean sql = new BookTrackerBean(); 
     String bookName = request.getParameter("bookName"); 
     String bookAuthor = request.getParameter("bookAuthor"); 
     String bookGenres = request.getParameter("bookGenres"); 
     String bookDesc = request.getParameter("bookDesc"); 
     String bookDueDate = request.getParameter("bookDueDate"); 
     String bookStatus = request.getParameter("bookStatus"); 
     String fullname = request.getParameter("fullname"); 
     int id = Integer.parseInt(id_str); 
     PrintWriter out = response.getWriter(); 
     out.println(id); 
     Date date = new Date(); 
     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
     try { 
      date = formatter.parse(bookDueDate); 
     } catch (ParseException e) { 

      e.printStackTrace(); 
     } 
     java.sql.Date sqlDate = new java.sql.Date(date.getTime()); 

     sql.updateBook(bookName, bookAuthor, bookGenres, bookDesc, sqlDate, 
       bookStatus, fullname, id); 

    } 

這一切都從抓住本頁面的ID開始,

<body> 
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
     url="jdbc:mysql://localhost:3306/BookTracker" user="root" password="school" /> 

    <sql:query dataSource="${snapshot}" var="result"> 
SELECT * from BookTrackerSystem; 
</sql:query> 


    <table border="1" width="100%"> 
     <tr> 
      <th>Book ID</th> 
      <th>Book Name</th> 
      <th>Book Author</th> 
      <th>Book Genre</th> 
      <th>Book Description</th> 
      <th>Book Due Date</th> 
      <th>Book Status</th> 
      <th>Full Name</th> 
     </tr> 
     <c:forEach var="row" items="${result.rows}"> 
      <tr> 
       <td><c:out value="${row.id}" /></td> 
       <td><c:out value="${row.bookName}" /></td> 
       <td><c:out value="${row.bookAuthor}" /></td> 
       <td><c:out value="${row.bookGenres}" /></td> 
       <td><c:out value="${row.bookDesc}" /></td> 
       <td><c:out value="${row.bookDueDate}" /></td> 
       <td><c:out value="${row.bookStatus}" /></td> 
       <td><c:out value="${row.fullname}" /></td> 
       <td><a href="updatebook.jsp?id=${row.id}">Update</a></td> 

      </tr> 
     </c:forEach> 
    </table> 
</body> 

奇怪的是,我遇到的是,我可以得到&打印'ID'完美,但在準備好的聲明它是空的。爲什麼?

這裏是當我點擊按鈕

UPDATE bookTrackerSystem SET bookname='aaaaaa', bookAuthor='', bookGenres='', bookDesc='', bookDueDate='1991-10-10', fullname='' WHERE id=null 
+0

在你的更新查詢中'UPDATE bookTrackerSystem SET bookname = ?, bookAuthor =?,bookGenres =?,bookDesc =?,bookDueDate =?,fullname =? WHERE id =?',圖書狀態丟失。在查詢中添加書籍狀態,然後重試。 – Apurv

+0

爲什麼從上面的代碼中刪除'update.setInt(8,id);'這行? – Apurv

+1

聰明!你刪除了前一個問題在您的PreparedStatement是錯誤的,現在你張貼另一個錯誤的PreparedStatement同樣的問題。 – NINCOMPOOP

回答

0

您還沒有提交ID後面,把它的隱藏字段的形式

0

在你updateBook方法執行PreparedStatement的,

update.setString(1, bookName); 
      update.setString(2, bookAuthor); 
      update.setString(3, bookGenres); 
      update.setString(4, bookDesc); 
      update.setDate(5, bookDueDate); 
      update.setString(6, bookStatus); 
      update.setString(7, fullname); 

我沒有看到你正在使用設置id的值的代碼,所以這是理由設置ID爲空,

如果添加

update.setLong(8號);

+0

你的意思是你不能在問題中發佈的代碼中看到'update.setInt(8,id);'? – Apurv

+0

沒有,因爲你沒有任何第八位把第8 pramater這不會是正確的。 –

0

ID不歌廳空。你在這裏使用了錯誤的變量。

update.setString(7, fullname); 

第7個位置是你的條件,你在那個條件下放置全名而不是id。

將其更改爲

update.setString(7, id); 
0

對於PreparedStatement你需要通過在同一順序的值,你在你的查詢需要他們。

所以對於你的陳述UPDATE bookTrackerSystem SET bookname=?, bookAuthor=?, bookGenres=?, bookDesc=?, bookDueDate=?, fullname=? WHERE id=?你傳遞錯誤的參數值6 & 7你沒有爲id添加任何參數,在你的情況下應該是第8個參數。

所以根據對第6和第7的參數更新你的代碼,並添加值id爲update.setString(8號);

相關問題