Back-end Development log (ii): Export of commodity information

Source: Internet
Author: User

In the school struggle for two weeks, currently able to achieve the function of login, registration, check the product information, by keyword query products, orders, according to the User name Inquiry order. Summer vacation to continue to make these features more friendly, but also to add shopping cart, any heavy and long way ...

Let's talk about something else before we get to the point. The registration section has some changes, the user name cannot be empty, cannot be duplicated with the database, must be 3-8-bit letters. This method exists in the implementation class related to the user, the code is as follows:

public int addUser (user user) throws Exception {int flag = 1; String sql = "SELECT * from UserInfo where username=?"; String sqladd= "INSERT into javawebdb.userinfo values (?,?)"; PreparedStatement pstmt = Null;dbconnect DBC = Null;//dbconnect is specifically used to connect to the database class try{//connection Database DBC = new Dbconnect ();p stmt = DBC.G Etconnection (). preparestatement (SQL);p stmt.setstring (1,user.getusername ());//query user name ResultSet rs = Pstmt.executequery ();//make the query info to personif (Rs.next ()) {if (Rs.getstring ("username"). Equals (                       User.getusername ())) {flag = 0; Cannot repeat}}if (user.getusername () = = NULL | | user.getusername (). Trim (). Equals ("")) {flag = 0;
Cannot be empty}if (!user.getusername (). Matches ("[a-za-z]{3,8}")) {flag = 0; Must be a 3-8-letter}if (flag = = 1) {pstmt = Dbc.getconnection (). Preparestatement (Sqladd);p stmt.setstring (1,user.getusername ( ));p stmt.setstring (2,user.getpassword ());p stmt.executeupdate ();//write new user information in the database}rs.close ();p stmt.close ();} catch (SQLException e) {System.out.println (E.getmessage ());} finally {//close dbdbc.close ();} return flag;}

Commodity information output is also based on the operation of the database, in a certain order in the table output database of all tuples in the line, but the teacher asked to finally implement in HTML, I also have to go to self-learning JS and Ajax only line, now the knowledge is not enough, can only display the results of the JSP page, sad =. =

Do not write the database related operations directly into the JSP file!

Do not write the database related operations directly into the JSP file!

Do not write the database related operations directly into the JSP file!

The important thing to say three times.

This is the method in the background implementation class:

Public ArrayList showproduct () throws Exception{string sql = "SELECT * from ProductInfo"; PreparedStatement pstmt = Null;dbconnect DBC = null; ArrayList ArrayList = new ArrayList () Try{dbc = new Dbconnect ();p stmt = Dbc.getconnection (). preparestatement (SQL); ResultSet rs = Pstmt.executequery ();      while (Rs.next ()) {        Product pro=new product ();        Pro.setid (Rs.getint (1));        Arraylist.add (0,pro.getid ()); When actually writing, there are a lot of attributes in product, here is only one example, product number                 } rs.close ();} catch (Exception ee) {ee.printstacktrace ();} finally {dbc.close ();}          return ArrayList; Note the type of the return value}   

Tried before in the ArrayList directly stored objects, but to the foreground output to become an address, very annoying, but will not solve, so the use of such access to the rough method of string. Seek advice and advice ...

Only Java code is listed in the JSP:

List List = (ArrayList) session.getattribute ("list"); Transfer the ArrayList.

for (int i=list.size () -1;i>=0;i=i--) {

Out.print (List.get (i));

}//the above method I only saved the product ID, so here also changed, only one column output

  

Back-end Development log (ii): Export of commodity information

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.