Writing SQL statements in JSP is the key

Source: Internet
Author: User

Recently, I wrote a JSP video. There is no big problem. Basically, I can understand it and implement it like a gourd. When I wrote an SQL query statement last night, I did not miss a field. It takes 10 minutes to locate the problem. It means that the sleep has been postponed for 10 minutes! Cup!

The specific function is to modify and save a table. Here, the table structure is (ID, name, level), where ID increases automatically. To modify and save the data on the page, you just need to retrieve the data from the table and return it to the display on the page. You can change the data based on the displayed content, and then submit and save the data.

When you enter the modification interface, you can call the data in the servlet to retrieve the data. The data retrieval code is as follows: (you can call this method)

Code:
  1. Public void precategory (httpservletrequest request,
  2. Httpservletresponse response) throws servletexception, ioexception {
  3. String id = request. getparameter ("ID ");
  4. String SQL = "select ID, name, level from category where id =" + ID;
  5. // String SQL = "Select name, level from category where id =" + ID; queries with fewer IDS
  6. CATEGORY Category = NULL;
  7. Queryrunner QR = dbhelper. getqueryrunner ();
  8. Try {
  9. List list = (list) QR. Query (SQL, new beanlisthandler (category. Class ));
  10. System. Out. println ("previous list =" + list. Size ());
  11. Category = (Category) list. Get (0 );
  12. } Catch (sqlexception e ){
  13. E. printstacktrace ();
  14. }
  15. System. Out. println ("previous ID =" + category. GETID ());
  16. System. Out. println ("name =" + category. getname ());
  17. System. Out. println ("level =" + category. getlevel () before uploading ());
  18. Request. setattribute ("category", category );
  19. Request. getrequestdispatcher ("/editcategory. jsp"). Forward (request, response );
  20. }

It seems that there is nothing wrong, but it is not successful when executing update insertion. It turns out that the id value here is null. I just knew it clearly through background printing. The comments are the SQL statements with errors, which I personally think are hidden.

After the submission, another database data insertion action is executed. If no error is reported, (insertion fails) The new page is returned. So we have to go back to this page along with the new page.

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.