Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ';id';参数作为null进入PreparedStatement_Java_Mysql_Jsp_Jdbc_Jstl - Fatal编程技术网

Java ';id';参数作为null进入PreparedStatement

Java ';id';参数作为null进入PreparedStatement,java,mysql,jsp,jdbc,jstl,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).

我在另一节课上有这个方法

  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>
这一切都是从从这个页面抓取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,请在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)

ID未获取空值。您在这里使用了错误的变量

update.setString(7, fullname);
第七个位置是您的where条件,您将全名放在该非id中

换成

update.setString(7, id);

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

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


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

在更新查询中,更新bookTrackerSystem设置bookname=?,bookAuthor=?,bookGenres=?,bookDesc=?,bookDueDate=?,fullname=?其中id=?,缺少书本状态。在查询中添加图书状态,然后重试。为什么删除行
update.setInt(8,id)来自您的上述代码?聪明!您删除了先前的问题,其中您的PreparedStatement是错误的,现在您将同一问题与另一个错误的PreparedStatement一起发布。您的意思是您无法看到此
update.setInt(8,id)在问题中张贴的代码中?不,这是不对的,因为你没有任何第八个位置来放置第八个pramater。
update.setString(7, fullname);
update.setString(7, id);