我有一個單獨的類此方法,'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
在你的更新查詢中'UPDATE bookTrackerSystem SET bookname = ?, bookAuthor =?,bookGenres =?,bookDesc =?,bookDueDate =?,fullname =? WHERE id =?',圖書狀態丟失。在查詢中添加書籍狀態,然後重試。 – Apurv
爲什麼從上面的代碼中刪除'update.setInt(8,id);'這行? – Apurv
聰明!你刪除了前一個問題在您的PreparedStatement是錯誤的,現在你張貼另一個錯誤的PreparedStatement同樣的問題。 – NINCOMPOOP