【问题标题】:'id' Parameter going into PreparedStatement as null'id' 参数作为 null 进入 PreparedStatement
【发布时间】:2013-04-16 04:53:58
【问题描述】:

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

  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

【问题讨论】:

  • 在您的更新查询UPDATE bookTrackerSystem SET bookname=?, bookAuthor=?, bookGenres=?, bookDesc=?, bookDueDate=?, fullname=? WHERE id=? 中,缺少图书状态。在查询中添加图书状态,然后重试。
  • 为什么要从上面的代码中删除update.setInt(8, id);这一行?
  • 聪明!您删除了上一个您的 PreparedStatement 错误的问题,现在您使用另一个错误的 PreparedStatement 发布了相同的问题。

标签: java mysql jsp jdbc jstl


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在你的 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);

    【讨论】:

    • 您的意思是您无法在问题中发布的代码中看到这个update.setInt(8, id);
    • 不,这是不对的,因为您没有任何第 8 位可以放置第 8 位 pramater。
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

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

      update.setString(7, fullname);
      

      第 7 位是你的 where 条件,你将全名放在那个不是 id 中。

      改成

      update.setString(7, id);
      

      【讨论】:

        猜你喜欢
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-27
        • 2012-07-14
        • 2011-04-12
        相关资源
        最近更新 更多