日期:2014-05-16 浏览次数:20401 次
ReckoningServlet代码如下: package servlet.user; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.Iterator; import javax.servlet.RequestDispatcher; 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 dao.DBConnection; import bean.Cart; import bean.CartItem; public class ReckoningServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); Cart cart = (Cart) session.getAttribute("cart"); if (cart == null || cart.getCart().size()==0) { request.setAttribute("null", "1"); } else { ArrayList login = (ArrayList) session.getAttribute("login"); String userId = (String) login.get(0); DBOperation dbo=new DBOperation(); ArrayList<CartItem> al = cart.getCart(); Iterator<CartItem> it = al.iterator(); String bookId = ""; int bookQuantity = 0; String SQL1 = ""; String SQL2 = ""; String SQL3 = ""; String SQL4 = ""; while(it.hasNext()){ CartItem cartItem = it.next(); bookId = cartItem.getId(); bookQuantity = cartItem.getQuantity(); SQL1 = "update books set storage=storage-"+bookQuantity+" where id = "+bookId+""; dbo.insertDeleteUpdate(SQL1); SQL2 = "insert into orders(userid,bookid,booksum,times) values ("+userId+","+bookId+","+bookQuantity+",Getdate())"; dbo.insertDeleteUpdate(SQL2); SQL3 = "select * from rank where bookid = "+bookId+""; if(dbo.queryReturnboolean(SQL3)){ SQL4 = "update rank set quantity=quantity+"+bookQuantity+" where bookid="+bookId+""; dbo.insertDeleteUpdate(SQL4); }else{ SQL4 = "insert into rank(bookid,quantity) values("+bookId+", "+bookQuantity+")"; dbo.insertDeleteUpdate(SQL4); } } request.setAttribute("ok", "1"); session.removeAttribute("cart"); } RequestDispatcher rd = request.getRequestDispatcher("/user/cart.jsp"); rd.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }