SubmitOrderServlet.java
public class SubmitOrderServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //该servlet要处理下订单的请求 try { OrderService orderservice=new OrderService(); MyCart myCart=(MyCart)request.getSession().getAttribute("myCart"); Users user=(Users)request.getSession().getAttribute("loginUser"); orderservice.submitOrder(myCart, user); } catch (Exception e) { e.printStackTrace(); request.getRequestDispatcher("/WEB-INF/errInfo.jsp"); // TODO: handle exception } //如果订单写入到数据库,把邮件发送给客户 SendMailToSomeone sts=new SendMailToSomeone(); Users user=(Users)request.getSession().getAttribute("loginUser"); System.out.println(user.getEmail()); sts.send("购物确认", "<meta http-equiv=content-Type content=text/html;charset=gb2312><div align=center><h1 style=\"color: red\">"+user.getName()+"在XX网订购了一份商品</h1></div>", user.getEmail(), "[email protected]", "wx", "smtp.sohu.com"); request.getRequestDispatcher("/WEB-INF/OrderOk.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); this.doGet(request, response); } }
调用了OrderService类
OrderService.java
package com.wxh.service; import java.sql.*; import java.util.ArrayList; import javax.management.RuntimeErrorException; import com.wxh.domain.Book; import com.wxh.domain.Users; import com.wxh.utils.DBUtil; //处理与订单相关的业务逻辑 public class OrderService { private Connection ct=null; private PreparedStatement ps=null; private ResultSet rs=null; //下订单涉及到两张表,而且两张表有关系 public void submitOrder(MyCart myCart,Users user){ String sql="insert into orders values(order_seq.nextval,?,?,sysdate)"; //因为添加订单复杂,所以这种操作很特别,于是自己写一个。 try { ct=DBUtil.getCon(); //为了保证订单号是稳定的,所以将其事务隔离级别升级(可串行) ct.setAutoCommit(false); ct.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); ps=ct.prepareStatement(sql); ps.setInt(1,user.getId()); ps.setFloat(2, user.getId()); ps.executeUpdate(); //如何得到刚刚插入的订单记录的订单号 sql="select order_seq.currval from orders"; ps=ct.prepareStatement(sql); rs=ps.executeQuery(); int orderId=0; if(rs.next()){ //取出刚刚生成的订单号 orderId=rs.getInt(1); } //把订单细节表生成(批量提交) ArrayList al=myCart.showMyCart(); for(int i=0;i<al.size();i++){ Book book=(Book)al.get(i); sql="insert into orderitem values(orderitem_seq.nextval,?,?,?)"; ps=ct.prepareStatement(sql); ps.setInt(1, orderId); ps.setInt(2, book.getId()); ps.setInt(3, book.getShoppingNum()); ps.executeUpdate(); } //整体提交 ct.commit(); } catch (Exception e) { // TODO: handle exception try { ct.rollback(); } catch (Exception e1) { // TODO: handle exception e1.printStackTrace(); } //e.printStackTrace(); throw new RuntimeException(e.getMessage()); }finally{ DBUtil.close(rs, ps, ct); } } }
——摘自《韩顺平细说jsp》
时间: 2024-11-05 19:39:18