简体   繁体   English

无法通过jsp使用PreparedStatement在数据库中插入数据

[英]Failed to insert data in database with PreparedStatement with jsp

I'm trying to insert data into database using PreparedStatement based on form input. 我正在尝试使用基于表单输入的PreparedStatement将数据插入数据库。 The program follows normal flow when user clicks "submit" on the checkout page (checkout.jsp) the form redirect him to the confirmation.jsp as wanted but no data gets inserted. 当用户单击checkout页面(checkout.jsp)上的“提交”时,程序将按照正常流程进行操作,该表单将根据需要将其重定向到confirmation.jsp,但没有插入任何数据。 When user submit forms, the program seems to ignore the PreparedStatement and only notices the userPath. 当用户提交表单时,该程序似乎忽略了PreparedStatement,仅注意到userPath。 I wonder what could be the cause of this behavior? 我想知道可能是这种行为的原因吗? No error gets printed. 没有错误被打印出来。

Servlet.java Servlet.java

        } else if (userPath.equals("/purchase")) {

       String OD = request.getParameter("OD");    
          String FN = request.getParameter("FN");  
          String LN = request.getParameter("LN");  
          String PN = request.getParameter("PN");  
          String EM = request.getParameter("EM");  
          String A1 = request.getParameter("A1");  
          String A2 = request.getParameter("A2");  
          String CT = request.getParameter("CT");  
          String SPR = request.getParameter("SPR");  
          String PZC = request.getParameter("PZC");  
          String CTRY = request.getParameter("CTRY");  
          String referenceNumber = request.getParameter("referenceNumber");  
          String status = request.getParameter("status");  
          String queue = request.getParameter("queue");  
          String cart_total = request.getParameter("cart_total");  
          String order_details = request.getParameter("order_details");  
try{
     Class.forName("com.mysql.jdbc.Driver");
           Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/application?user=root&password=12345");
     PreparedStatement pst;
     pst = connection.prepareStatement("insert into customer_order(date_created, first_name,last_name,phone,email,address_1,address_2,city,State_Province_Region,Postal_Zip_Code,country,referenceNumber,status,queue,cart_total,order_details) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
     //When concating strings you should do something like st = connection.prepareStatement("Insert into student values ('" + userid + "','"+pw+"')"); but the right way is
     //st = connection.prepareStatement("Insert into student values (1,2)");
       pst.setString(1,OD);
                    pst.setString(2,FN);
                    pst.setString(3,LN);
                    pst.setString(4,PN);
                    pst.setString(5,EM);
                    pst.setString(6,A1);
                    pst.setString(7,A2);
                    pst.setString(8,CT);
                    pst.setString(9,SPR);
                    pst.setString(10,PZC);
                    pst.setString(11,CTRY);
                    pst.setString(12,referenceNumber);
                    pst.setString(13,status);
                    pst.setString(14,queue);
                    pst.setString(15,cart_total);
                    pst.setString(16,order_details);

     pst.executeUpdate();
}catch(ClassNotFoundException | SQLException ex){
     //print a message or something
}finally{
           Connection connection = null;
     if (connection!=null){
               try {
                   connection.close();
               } catch (SQLException ex) {
                   Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
               }
     }
}
            userPath = "/confirmation";
        }

Checkout.jsp Checkout.jsp

<form  id="shipping_form"  method="post" action="purchase" name="checkout">

                                <label for="date" class="shp_label">Order Date </label>
                                <input id="date" name="OD" type="text" class="shp_ipt" value="<%= new java.util.Date()%>" readonly>

                                <label for="fname" class="shp_label">First Name   </label>
                                <input id="fname" name="FN" type="text" class="shp_ipt" required>


                                <label for="lname" class="shp_label">Last Name   </label>
                                <input id="lname" name="LN" type="text" class="shp_ipt" required>

                                <label for="phone" class="shp_label"> Phone  </label>
                                <input id="phone" name="PN" type="text" class="shp_ipt" required>

                                <label for="email" class="shp_label"> Email   </label>
                                <input id="email" name="EM"  type="email" class="shp_ipt" required>

                                <label for="add1" class="shp_label"> Address 1  </label>
                                <input id="add1" name="A1" type="text" class="shp_ipt" required>


                                <label for="add2" class="shp_label"> Address 2   </label>
                                <input id="add2" name="A2" type="text" class="shp_ipt">

                                <label for="city" class="shp_label"> City   </label>
                                <input id="city" name="CT"  type="text" class="shp_ipt" required>


                                <label for="spr" class="shp_label"> Region  </label>
                                <input id="spr" name="SPR" type="text" class="shp_ipt" required>


                                <label for="pzc" class="shp_label"> Zip / Code </label>
                                <input id="pzc" name="PZC" type="text" class="shp_ipt" required>

                                <label for="country" class="shp_label"> Country </label>
                                <input id="country"name="CTRY"  type="text" class="shp_ipt" required>


<input type="hidden" name="referenceNumber" value="<%=System.currentTimeMillis()%>" />
<input type="hidden" name="staus" value="Pending" />
<input type="hidden" name="queue" value="This information will be updated soon." />
<input type="hidden" class="cartTotal"  style="border:none; background:transparent;" name="cart_total" readonly type="text" value="${cart.total}"/>
<input type="hidden" name="order_details" value="Your order is being processed." />                                


<input class="submit_button button" name="submit" value="Ship Me!" type="submit">



                            </form>

If you have no error found then you can change some code in preparedStatement . 如果没有发现错误,则可以在prepareStatement中更改一些代码。 Like this - 像这样 -

pst = connection.prepareStatement("insert into customer_order values('"+FN+"', '"+LN+"',.........) "); pst = connection.prepareStatement(“插入到customer_order值中('” + FN +“','” + LN +“',.........)”);

And one think remember that after PreparedStatement it is must be perform execute() method , by PreparedStatement's object . 有人想到,在PreparedStatement之后,必须通过PreparedStatement的对象执行execute()方法。 Like this - pst.execute(); 这样-pst.execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM