文章目录
一、案例演示二、实现步骤1.建表2.order类3.orderItems类4.OrderDao3.OrderServlet5.order.jsp一、案例演示
当我们点击info.jsp的结账时,页面跳转到order.jsp结算中心页面,首先在数据库中创建orders表和orderItems表用于存放账单数据,在java中建orders.java和orderItems.java两个java类用于获取和设置数据,建OrderDao.java实现添加数据的方法,建OrderServlet当act=add时添加订单
二、实现步骤
1.建表
orders表和orderItems表
create table orders (id int primary key auto_increment,money double,receiverAddress varchar(255) default null,receiverName varchar(20) default null,receiverPhone varchar(20) default null,paystate int(11) default 0,ordertime varchar(50) default null ,user_id int(11))engine=InnoDb default charset=utf8;create table orderItems (order_id int default null ,book_id int default null ,buynum int default null)engine=InnoDb default charset=utf8;alter table orderItems add foreign key(order_id) references orders(id);alter table orderItems add foreign key(book_id) references books(id);insert into orders(money) values(200);insert into orderItems (buynum) values(10);select *from orders;select *from orderItems;
2.order类
public class Order {//订单表private int id;//订单编号private double money;//订单总价private String receiverAddress; //送货地址private String receiverName;//收货人姓名private String receiverPhone;//收货人电话private int paystate;//订单状态private String ordertime;//下单时间private Users user_id;//订单所属用户private List<OrderItem> orderItems = new ArrayList<OrderItem>();public int getId(){return id;}public void setId(int id) {this.id = id;}public double getMoney() {return money;}public void setMoney(double money) {this.money = money;}public String getReceiverAddress() {return receiverAddress;}public void setReceiverAddress(String receiverAddress) {this.receiverAddress = receiverAddress;}public String getReceiverName() {return receiverName;}public void setReceiverName(String receiverName) {this.receiverName = receiverName;}public String getReceiverPhone() {return receiverPhone;}public void setReceiverPhone(String receiverPhone) {this.receiverPhone = receiverPhone;}public int getPaystate() {return paystate;}public void setPaystate(int paystate) {this.paystate = paystate;}public String getOrdertime() {return ordertime;}public void setOrdertime(String ordertime) {this.ordertime = ordertime;}public Users getUser_id() {return user_id;}public void setUser_id(Users user_id) {this.user_id = user_id;}public List<OrderItem> getOrderItems() {return orderItems;}public void setOrderItems(List<OrderItem> orderItems) {this.orderItems = orderItems;}}
3.orderItems类
public class OrderItem {//订单项目表private Order order;private Books b;private int buynum;public Order getOrder() {return order;}public void setOrder(Order order) {this.order = order;}public Books getB() {return b;}public void setB(Books b) {this.b = b;}public int getBuynum() {return buynum;}public void setBuynum(int buynum) {this.buynum = buynum;}}
4.OrderDao
public class OrderDao {//添加订单public boolean add(Order o) {Connection con = null;PreparedStatement pst = null;ResultSet rs = null;try {con = JDBCutils.getConnection();//关闭事物的自动提交con.setAutoCommit(false);//向订单表里添加数据String sql = "insert into orders (money,receiverAddress,receiverName,receiverPhone,ordertime,user_id) values(?,?,?,?,?,?)";pst = con.prepareStatement(sql);pst.setDouble(1, o.getMoney());pst.setString(2, o.getReceiverAddress());pst.setString(3, o.getReceiverName());pst.setString(4, o.getReceiverPhone());pst.setString(5, o.getOrdertime());pst.setInt(6, o.getUser_id().getId());int i = pst.executeUpdate();if (i > 0) {sql = "select id from orders order by id desc limit 1";pst = con.prepareStatement(sql);rs= pst.executeQuery();if (rs.next()) {o.setId(rs.getInt("id"));} else {//如果失败回退事务con.rollback();return false;}String sql2="insert into orderItems(order_id,book_id,buynum) values(?,?,?)";pst=con.prepareStatement(sql2);List<OrderItem> items= o.getOrderItems();for (OrderItem oi:items){pst.setInt(1,o.getId());pst.setInt(2,oi.getB().getId());pst.setInt(3,oi.getBuynum());int i2=pst.executeUpdate();if(i2<=0){con.rollback();break;}}String sql3="update books set num=num-? where id=?";pst=con.prepareStatement(sql3);for (OrderItem oi:items){pst.setInt(1,oi.getBuynum());pst.setInt(2,oi.getB().getId());int i3=pst.executeUpdate();if(i3<0){con.rollback();break;}}}if(i>0){//提交事务mit();return true;}else {return false;}} catch (Exception e) {e.printStackTrace();}finally {try {con.setAutoCommit(true);JDBCutils.close(con,pst,rs);}catch (SQLException e){e.printStackTrace();}}return false;}}
3.OrderServlet
if(act.equals("add")){//得到当前用户HttpSession session=request.getSession();Users user = (Users) session.getAttribute("user");//从购物车里获取商品信息Map<Books,Integer> cart= (Map<Books, Integer>) session.getAttribute("cart");//封装数据到订单对象中Order order =new Order();order.setUser_id(user);order.setReceiverAddress(request.getParameter("receiverAddress"));order.setReceiverName(request.getParameter("receiverName"));order.setReceiverPhone(request.getParameter("receiverPhone"));order.setMoney(Double.parseDouble(request.getParameter("money")));Date date =new Date();SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");order.setOrdertime(sdf.format(date));for (Books b:cart.keySet()){OrderItem item =new OrderItem();item.setOrder(order);item.setB(b);item.setBuynum(cart.get(b));order.getOrderItems().add(item);}//添加订单的方法OrderDao od=new OrderDao();if(od.add(order)){System.out.println("add success");}else{System.out.println("add error");}}
5.order.jsp
<form id="orderForm" action="${pageContext.request.contextPath}/OrderServlet?act=add" method="post">