'id'参数进入PreparedStatement为null

时间:2013-04-16 04:53:58

标签: java mysql jsp jdbc jstl

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

  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>

奇怪的是,我遇到的是,我可以得到&amp; print'id'完美但在准备好的语句中它将为null。为什么呢?

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

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

4 个答案:

答案 0 :(得分:0)

您没有提交身份证明,请将其隐藏在表单

答案 1 :(得分: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);

答案 2 :(得分:0)

ID未获取null。你在这里使用了错误的变量。

update.setString(7, fullname);

第7个位置适用于您所处的条件,并且您将全名放在不是id的位置。

将其更改为

update.setString(7, id);

答案 3 :(得分:0)

对于PreparedStatement,您需要在查询中按照所需的顺序传递值。

因此,对于您的陈述UPDATE bookTrackerSystem SET bookname=?, bookAuthor=?, bookGenres=?, bookDesc=?, bookDueDate=?, fullname=? WHERE id=?,您传递的参数为6&amp; 7并且你没有为id添加任何参数,在你的情况下它应该是第8个参数。

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

相关问题