【问题标题】:Failed insert multiple rows into tables using prepareStatement in controller使用控制器中的 prepareStatement 向表中插入多行失败
【发布时间】:2016-02-11 16:35:39
【问题描述】:

我的代码如下所示。我试图从 JSP 页面获取多个输入并将它们插入到控制器中的 SQL 中。但它不起作用。谁能指出我正确的方向?

String price[] = request.getParameterValues("price");
String isbn[] = request.getParameterValues("isbn");
String title[] = request.getParameterValues("title");
String authors[] = request.getParameterValues("authors");

HttpSession session = request.getSession();
int number = (Integer)session.getAttribute("number");

for(int i = 0; i<number;i++){
     String queryAdd = "INSERT INTO books (price, isbn, title,authors) values" + "(?,?,?,?)";
     PreparedStatement stmt = conn.prepareStatement(queryAdd);
     stmt.setFloat(1, Float.valueOf(price[i]));
     stmt.setString(2, isbn[i]);
     stmt.setString(3, title[i]);
     stmt.setString(4, authors[i]);
     int result = stmt.executeUpdate(queryAdd);
     out.println(result);
     if(result<=0){
         RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/Views/error.jsp");
         rd.forward(request, response);
     }
}
conn.close();
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/Views/successful.jsp");
rd.forward(request, response);

我的jsp页面在这里:

<form action="addBook.htm" method="post">
    <table style="border:1px solid red; border-collapse: collapse">
        <tr>
            <th>ISBN</th>
            <th>Book Title</th>
            <th>Authors</th>
            <th>Price</th>
        </tr>
        <c:forEach var="i" begin="1" end="${sessionScope.number}" step="1">
            <tr>
                <td><input type="text" name="isbn"/></td>
                <td><input type="text" name="title"/></td>
                <td><input type="text" name="authors"/></td>
                <td><input type="text" name="price"/></td>
            </tr>
        </c:forEach>
        <tr>
            <td colspan="4" style="text-align: center">
                <input type="submit" name="submit" value="Add Books" />
                <input type="hidden" name="action" value="addSubmit"/>
             </td>
        </tr>
    </table>
</form>

【问题讨论】:

  • 首先你需要知道request.getParameterValues("price");里面是什么,其次你的jsp文件中的名字应该是&lt;input type="text" name="isbn[]"/&gt;所以它会发送一个数组
  • 我的jsp文件有问题吗?我没有从 jsp 文件中得到正确的数据?
  • 是的,你的jsp文件是错误的,如果你这样做,只会发送1个值(最后一个),但控制器也可能是错误的,我忘记在java中初始化数组.你能在你修复你的jsp文件后发布这个System.out.println(request.getParameterValues("price"));的结果吗
  • 其实,在我的原始代码中,它可以获取所有输入并发送到servelt,而不仅仅是最后一个。我在调试模式下通过测试确认了这一点,也许我认为问题不在于。
  • 刚刚发现我犯了一个愚蠢的错误,连接到错误的数据库..

标签: sql jsp model-view-controller jdbc prepared-statement


【解决方案1】:

您的PreparedStatement 调用有问题:

 String queryAdd = "INSERT INTO books (price, isbn, title,authors) values (?,?,?,?)";
 PreparedStatement stmt = conn.prepareStatement(queryAdd);
 stmt.setFloat(1, Float.valueOf(price[i]));
 stmt.setString(2, isbn[i]);
 stmt.setString(3, title[i]);
 stmt.setString(4, authors[i]);

到目前为止很好 - 但不是下一行:

 int result = stmt.executeUpdate(queryAdd);

这会调用StatementexecuteUpdate 方法——传入SQL 并忽略所有参数集。改成

 int result = stmt.executeUpdate();

您可能会看到一些结果。

关于您的代码的更多注释:

  • 如果数组中的项目不多,则应考虑 addBatch() 和 executeBatch() 减少 SQL 往返。
  • 关闭您的语句/使用 try-with-resource

【讨论】:

  • @user5516371 如果答案对您有帮助,您可以接受/投票。这会很好。
猜你喜欢
  • 2013-11-11
  • 2016-08-04
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多