Do the course design time to do a bit annoying so just write a little bit below is the code that will add to the shopping cart item dynamic display to the foreground page
First of all, my head.jsp. The "My Directory" in this application is not a direct link but a servlet name called "Selectusercart"
The user, books, and useraccout classes that I use inside are the basic classes that record the basic information for which the setxxx () and GetXXX () methods are implemented
<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Then there are the two classes I used:
Search my Shopping Cart tool class: Mycartutil.java
<pre name= "code" class= "java" >package mycart;import java.sql.connection;import java.sql.PreparedStatement; Import Java.sql.resultset;import java.sql.sqlexception;import java.util.arraylist;import bookConnUtil.DBUtil; Import Bookconnutil.books;public class Mycartutil {public static arraylist<mycart> list = new arraylist<mycart& gt; ();p ublic static arraylist<books> Booklist = new arraylist<books> ();p ublic static String msg;public static int countbook = 0;//public static mycart[] cart = new MYCART[10];p ublic static int count = 0;public static Boolean Insertintomycart (String username,string bookname,string pianduanname) throws sqlexception{string CartInsert = "Insert Into UserAccount valus (?,?,?) "; Boolean flag = false; Connection Conn=null; PreparedStatement stmt=null;try{conn = dbutil.getconnection (dbutil.connection_sql); stmt = Conn.prepareStatement ( Cartinsert); stmt.setstring (1, username); stmt.setstring (2, BookName); Stmt.setstring (3, pianduanname); int count = Stmt.eXecuteupdate (); if (count>0) {flag=true;} Else{flag=false;}} catch (Exception e) {System.out.println ("True Sorry, your program went wrong"); E.printstacktrace ();} Finally{conn.close (); Stmt.close ();} return flag;} public static Boolean Selectusercart (String userName) throws Sqlexception{boolean flag = false; String cartselect = "Select Username,bookname,pianduanname,imagepath,price from useraccount where username =?"; Connection Conn=null; ResultSet Rs=null; PreparedStatement stmt=null;try{conn = dbutil.getconnection (dbutil.connection_sql); stmt = Conn.prepareStatement ( Cartselect); stmt.setstring (1, userName); rs = Stmt.executequery (); while (Rs.next ()) {Mycart cart = new Mycart (); Cart.setusername (rs.getstring (1)), Cart.setbookname (Rs.getstring (2)), Cart.setpianduanname (Rs.getString (3)); Cart.setimagepath (Rs.getstring (4)); Cart.setprice (Rs.getint (5)); List.add (cart); count++;} System.out.println ("++++++++++++++++" +count+ "+++++++++++++"); if (count==0) {msg = "null";} Else{msg = "full";} Flag = true;} catch (Exception e) {System.out.println ("Really sorry, your program went wrong"); E.printstacktrace ();} Finally{conn.close (); Stmt.close ();} return flag;} public static Boolean Selectallbooks () throws Sqlexception{boolean flag = false; String cartselect = "Select Bookid,bookname,pianduanname,imagespath,price from Books"; Connection Conn=null; ResultSet Rs=null; PreparedStatement stmt=null;try{conn = dbutil.getconnection (dbutil.connection_sql); stmt = Conn.prepareStatement ( Cartselect); rs = Stmt.executequery (); while (Rs.next ()) {Books book = new books (); Book.setbookid (rs.getstring (1)); Book.setbookname (rs.getstring (2)), Book.setpianduanname (Rs.getstring (3)), Book.setimagepath (Rs.getString (4)); Book.setprice (Rs.getint (5)); Booklist.add (book);<span style= "White-space:pre" ></span>//I didn't do a good job, so I want to do it better with this mark. Please implement countbook++ yourself;} System.out.println ("++++++++++++++++" +count+ "+++++++++++++"); if (count==0) {msg = "null";} Else{msg = "full";} Flag = true;} catch (Exception e) {System.out.println ("Really Sorry, your program went wrong"); E.printstacktrace ();} Finally{conn.close (); Stmt.close ();} return flag;}}
Here is the implementation database connection class:
<pre name= "code" class= "java" >package bookconnutil;import java.sql.connection;import Java.sql.DriverManager; Import Java.sql.sqlexception;public class Dbutil {public static final int connection_mysql=2;public static final int Conne Ction_sql=1;public static final int connection_odbc=0;public Dbutil () {}public static CONNECTION getconnection (int Connection_type) throws Exception{switch (Connection_type) {case Connection_odbc:return getconnectionodbc (); case Connection_sql:return Getconnectionsql (); case Connection_mysql:return getconnectionmysql ();} return null;} /** * ?????????? */private static Connection Getconnectionodbc () throws Exception{connection Conn=null;try{class.forname (" Sun.jdbc.odbc.JdbcOdbcDriver "); SYSTEM.OUT.PRINTLN ("abc"); Conn=drivermanager.getconnection ("Jdbc:odbc:mybook", "sa", "123"); SYSTEM.OUT.PRINTLN ("Connection succeeded");} catch (Exception e) {e.printstacktrace (); throw e;} Return conn;} private static Connection Getconnectionsql () throws Exception{connection Conn=null; String ConnectionurL = "jdbc:sqlserver://localhost:1433;databasename=user;"; Try{class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); Conn=drivermanager.getconnection ( Connectionurl, "sa", "123"); SYSTEM.OUT.PRINTLN ("Connection succeeded");} catch (Exception e) {e.printstacktrace (); throw e;} Return conn;} private static Connection Getconnectionmysql () throws exception{string Connectionurl = "jdbc:mysql://localhost:3306/ User "; String username = "root"; String password = "MySQL"; Connection conn=null;try{class.forname ("Com.mysql.jdbc.Driver"); Conn=drivermanager.getconnection (Connectionurl, Username,password); SYSTEM.OUT.PRINTLN ("Connection succeeded");} catch (Exception e) {e.printstacktrace (); throw e;} Return conn;} public static void Close (Connection conn) throws Exception{if (Conn!=null) {try{conn.close ();} catch (SQLException e) {e.printstacktrace (); throw e;}}}}
The following is the implementation of the user shopping cart search class:
<pre name= "code" class= "java" >package mycart;import java.io.ioexception;import Java.sql.sqlexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Bookconnutil.bookdao;public class Selectusercart extends HttpServlet {/** * */ Private static final Long serialversionuid = 1l;public int count = Mycartutil.count; protected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {//TODO Auto-generated method Stubhttpsession session=req.getsession (); String username = (string) session.getattribute ("username"); try {//system.out.println ("not yet executed to the query process here"); Boolean flag= Mycartutil.selectusercart (username), if (flag==true) {/*for (Mycart message:myCartUtil.list) {for (int i=0;i<3;i++) {String A = Message.getusername ();}} *///session.setattribute ("Count", Countnum), if (MyCartUtil.msg.equals ("null")) {Session.setattribute ("null", mycartutil.msg);} Else{session.setattribute ("full", mycartutil.msg);} Req.getrequestdispatcher ("selectmyacccount.jsp"). Forward (req, resp);} Else{req.getrequestdispatcher ("error.html"). Forward (req, resp);}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
My Directory Implementation code:
<pre name= "code" class= "HTML" ><%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><% String path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dynamically generated pages (dynamically extracting data from the Database Generation page)