从 jsp 将多行插入数据库 mysql

Insert Multiple row into database mysql from jsp

将从我的数据库中生成几行并显示在我的 jsp 页面中。我放置了 1 个提交按钮,一旦用户单击提交按钮,我想将 jsp 页面中显示的数据插入 MySQL table。我在这个论坛中搜索了答案,并尝试按照给出的一些解决方案进行操作,但我收到此错误:java.lang.ArrayIndexOutOfBoundsException: 1,当我搜索错误时,它会将我带到 po.setAddress 处的控制器(地址[i]);

           po = new PaidOrder();
           po.setId(pid[i]);
           po.setPname(pname[i]);
           po.setQuantity(quantity[i]);
           po.setPrice(price[i]);
           po.setStatus(status[i]);
           po.setAddress(address[i]);
           po.setUser_email(user_email[i]); 

完整代码如下:

JSP

<form action="PaidOrderController" method="post">
<table class="timetable_sub">
   <thead>
       <tr>
          <th>Product Name</th>
          <th>Quantity</th>
          <th>Price</th>
          <th>Total Price</th>
          <th>Action</th>
        </tr>
    </thead>
    <tbody>
    <%
        OrderDAO dao = new OrderDAO();
        List<Order> orderList = dao.getAllOrderByEmail(user);
        DecimalFormat df = new DecimalFormat("#0.00");
        for (Order o : orderList) {
     %>
        <tr class="rem1">
          <td class="invert"><%= o.getPname() %></td>
          <td class="invert"><%= o.getQuantity() %></td>
          <td class="invert">RM <%= df.format(o.getPrice()) %></td>
          <td class="invert">RM <%= df.format(o.getQuantity()*o.getPrice()) %></td>
          <td>
              <a href="EditCartController?action=edit&id=<%= o.getId()%>"><button type="button" class="btn btn-success">Edit</button></a>
              <a href="DeleteFromCartController?action=delete&id=<%= o.getId()%>"><button type="button" class="btn btn-danger">Delete</button></a>
          </td>
          <input type="hidden" name="id" value="<%= o.getId() %>">
          <input type="hidden" name="pname" value="<%= o.getPname() %>">
          <input type="hidden" name="quantity" value="<%= o.getQuantity() %>">
          <input type="hidden" name="price" value="<%= o.getQuantity()*o.getPrice() %>">
          <input type="hidden" name="status" value="paid">
          <input type="hidden" name="user_email" value="<%=user%>">
       </tr>
          <%
             }
          %>
<div class="form-group" style="margin-top:20px">
    <label for="exampleFormControlTextarea1">Please fill up your address for postage below:</label>
    <textarea class="form-control" name="address" rows="3"></textarea>
</div>
<div class="checkout-right-basket">
    <button type="submit">Make a Payment<span class="far fa-hand-point-right"></span></button>
</div>
</form>

PaidOrder 模型中的代码

public class PaidOrder {
    private String id;
    private String pname;
    private String quantity;
    private String price;
    private String status;
    private String address;
    private String user_email;

    public void setId(String id) {
        this.id = id;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setUser_email(String user_email) {
        this.user_email = user_email;
    }

    public String getId() {
        return id;
    }

    public String getPname() {
        return pname;
    }

    public String getQuantity() {
        return quantity;
    }

    public String getPrice() {
        return price;
    }

    public String getStatus() {
        return status;
    }

    public String getAddress() {
        return address;
    }

    public String getUser_email() {
        return user_email;
    }

}

PaidOrderController

@WebServlet(name = "PaidOrderController", urlPatterns = {"/PaidOrderController"})
public class PaidOrderController extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PaidOrder po;
        List<PaidOrder> listPaidOrder= new ArrayList<>();

        String[] pid = request.getParameterValues("id");
        String[] pname = request.getParameterValues("pname");
        String[] quantity = request.getParameterValues("quantity");
        String[] price = request.getParameterValues("price");
        String[] status = request.getParameterValues("status");
        String[] user_email = request.getParameterValues("user_email");

        for(int i = 0; i < pid.length; i++){
            po = new PaidOrder();
            po.setId(pid[i]);
            po.setPname(pname[i]);
            po.setQuantity(quantity[i]);
            po.setPrice(price[i]);
            po.setStatus(status[i]);
            po.setUser_email(user_email[i]);

            listPaidOrder.add(po);

        }

        PaidOrderDAO.paidOrder(listPaidOrder);
    }

PaidOrderDAO

public static void paidOrder(List<PaidOrder> listPaidOrder){
        try(Connection conn = DBConnectionUtil.getConnection()) {

           String sql="INSERT INTO paid_orders(pname,quantity,price,status,user_email) VALUES (?,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql); 

            for(PaidOrder o:listPaidOrder){
                ps.setString(1, o.getPname());
                ps.setString(2, o.getQuantity());
                ps.setString(3, o.getPrice());
                ps.setString(4, o.getStatus());
                ps.setString(5, o.getUser_email());
                ps.executeUpdate();
            }

         } catch (Exception e){
            System.out.println("OrderDAO error"+ e.getMessage());
        }
    }

我希望有人能帮我解决这个问题:(

您收到的错误来自 po.setAddress(address[i]);,因为您在表格中只有一个地址。将其从 for 循环中删除,它将解决您的问题。

您需要更改 processRequestpaidOrder 的实现以保存地址,例如processRequest 的实现可以更改为

@WebServlet(name = "PaidOrderController", urlPatterns = {"/PaidOrderController"})
public class PaidOrderController extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PaidOrder po;
        List<PaidOrder> listPaidOrder= new ArrayList<>();

        String[] pid = request.getParameterValues("id");
        String[] pname = request.getParameterValues("pname");
        String[] quantity = request.getParameterValues("quantity");
        String[] price = request.getParameterValues("price");
        String[] status = request.getParameterValues("status");
        String[] user_email = request.getParameterValues("user_email");

        String address = request.getParameter("address");           

        for(int i = 0; i < pid.length; i++){
            po = new PaidOrder();
            po.setId(pid[i]);
            po.setPname(pname[i]);
            po.setQuantity(quantity[i]);
            po.setPrice(price[i]);
            po.setStatus(status[i]);                
            po.setUser_email(user_email[i]);

            listPaidOrder.add(po);
        }

        PaidOrderDAO.paidOrder(listPaidOrder, address);
    }
}

因此,paidOrder 的实施必须更改为:

public static void paidOrder(List<PaidOrder> listPaidOrder, String address){
    try(Connection conn = DBConnectionUtil.getConnection()) {

       String sql="INSERT INTO paid_orders(pname,quantity,price,status,user_email,address) VALUES (?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql); 

        for(PaidOrder o:listPaidOrder){
            ps.setString(1, o.getPname());
            ps.setString(2, o.getQuantity());
            ps.setString(3, o.getPrice());
            ps.setString(4, o.getStatus());
            ps.setString(5, o.getUser_email());
            ps.setString(6, address);            
            ps.executeUpdate();
        }

     } catch (Exception e){
        System.out.println("OrderDAO error"+ e.getMessage());
    }
}

如果您需要任何进一步的帮助,请随时发表评论。