Java Daniel Nurturance (6)-Error submitting an order to the database (Could not insert)

Source: Internet
Author: User
Tags date1


Background: SSH Video is really a powerful video that involves a wide range of knowledge, in line with today's internet trends. The teacher in the video is clear-minded, and the analysis needs are quite good. Of course, follow the idea of this video to go, you can easily understand the content inside. But then go can make mistakes, I am also drunk, the following describes my wonderful experience.


First, the problem was born


When the order is submitted to the database, the following error occurred, saying that the data can not be inserted into the database, at the beginning of the thought is the wrong to write, repeatedly query multiple times, but from Vo to DAO to service to action, in the JSP page, Applicationcontext.xml and Struts.xml are not wrong, what is going on???



Second, the solution


After communicating with the ancestors, it is understood that the reason why you cannot insert into a database is that the timestamp is not generated correctly when the order is generated. Later decisively replaced the original code, and then succeeded, when submitting the order at the same time inserted into two tables (order form and OrderItem table), Bang Bang!


1. Write a method to get time in Orderaction.java

Gets the time public static date Getnowdate () throws Java.text.ParseException  {Date currenttime = new Date (); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String datestring = Formatter.format (currenttime);D ate date1=formatter.parse (datestring); return date1;}



2.in the Orderaction.javaCall the Get time method to get the current time when the order was generated

The method to generate the order public String Save () throws PARSEEXCEPTION{//1, save data to database//order data of the complete Operation Order.setordertime (Getnowdate ()); O Rder.setstate (1); 1: Pending Payment 2: already paid, but no delivery 3: Already shipped, no confirmation receipt 4: Transaction complete//Total data is cart cart in information cart = (cart) servletactioncontext.getrequest (). Getsessi On (). getattribute ("cart"), if (cart = = null) {This.addactionerror ("Pro, you have not yet shopped, please go shopping first!") "); return" MSG ";} Order.settotal (Cart.gettotal ());//Set Order entry in order for (Cartitem CartItem:cart.getCartItems ()) {OrderItem OrderItem = new OrderItem (); Orderitem.setcount (Cartitem.getcount ()); Orderitem.setsubtotal (Cartitem.getsubtotal ()); O Rderitem.setproduct (Cartitem.getproduct ()); Orderitem.setorder (order); Order.getorderitems (). Add (OrderItem);} User Existuser = (user) servletactioncontext.getrequest (). GetSession (). getattribute ("Existuser"); Existuser = = null) {This.addactionerror ("Pro, you are not logged in, please login first!") "); return" Login ";} Order.setuser (Existuser); Orderservice.save (order);//2. Display the Order object on the page//by means of a value stack: Because the order displays objects that are model-driven// Empty cart Cart.clearcart (); return "Savesuccess";}



third, the effect of submission

Review the database after submitting the code, haha, the order data is duly in the database.




Iv. Learning Experience


1, sometimes it may not be your fault, but you can find a reasonable solution for the errors encountered.

2, the importance of communication, no matter is not the communication can not solve the key factors to solve the problem in communication.

3, encounter problems not discouraged, is a good attitude to life.






Java Daniel Nurturance (6)-Error submitting an order to the database (Could not insert)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.