0

我在一个单独的类中有这个方法,

  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”,但在准备好的语句中它会为空。为什么?

这是我单击按钮时执行的 PreparedStatement

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

4 回答 4

0

对于PreparedStatement您需要以与查询中需要它们相同的顺序传递值。

因此,对于您的语句,UPDATE bookTrackerSystem SET bookname=?, bookAuthor=?, bookGenres=?, bookDesc=?, bookDueDate=?, fullname=? WHERE id=?您为参数 6 和 7 传递了错误的值,并且您没有为 id 添加任何参数,在您的情况下这应该是第 8 个参数。

因此,根据第 6 个和第 7 个参数更新您的代码,并将 id 值添加为 update.setString(8, id);

于 2013-04-16T05:07:04.417 回答
0

你没有提交 id ,把它放在表单的隐藏字段中

于 2013-04-16T04:58:38.047 回答
0

在您的 updateBook 方法中,

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 设置为 null 的原因,

如果你添加

update.setLong(8, id);

于 2013-04-16T05:00:01.423 回答
0

ID 不会为空。您在这里使用了错误的变量。

update.setString(7, fullname);

第 7 位适用于您的 where 条件,并且您将全名放在那个不是 id 中。

将其更改为

update.setString(7, id);
于 2013-04-16T05:01:45.950 回答