www.pudn.com > restrant.zip > CheckoutDao.java, change:2014-04-13,size:3113b


package com.alex.restrantol.dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.Iterator; 
import java.util.Map; 
 
import com.alex.restrantol.base.BaseDao; 
import com.alex.restrantol.entity.OrderEntity; 
import com.alex.restrantol.util.MealItem; 
 
public class CheckoutDao { 
	Connection con; 
	PreparedStatement ps; 
	ResultSet rs; 
	 
	public boolean addOrder(OrderEntity order) { 
		boolean flag = false; 
		String orderId = null; 
		con = new BaseDao().getConn(); 
		try { 
			while(true){ 
				orderId = String.valueOf(Math.random()); 
				orderId = orderId.substring(2); 
				if(!(this.isOrderIdExist(orderId))){ 
					break; 
				} 
			} 
			String sql = "insert into orderline(customerId,customerName,customerTel,customerEmail,customerAddress,taste,remark,totalprice,status,orderId,overBookingDate)" + 
					" values(?,?,?,?,?,?,?,?,?,?,now())"; 
			ps = con.prepareStatement(sql); 
			ps.setInt(1, order.getCustomerId()); 
			ps.setString(2, order.getName()); 
			ps.setString(3, order.getTel()); 
			ps.setString(4, order.getEmail()); 
			ps.setString(5, order.getAddress()); 
			ps.setString(6, order.getTaste()); 
			ps.setString(7, order.getRemark()); 
			ps.setFloat(8, order.getTotalPrice()); 
			ps.setInt(9, order.getStatus()); 
			ps.setString(10, orderId); 
//			ps.setString(11, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis()))); 
//			ps.setDate(11, new java.sql.Date( new java.util.Date().getTime())); 格式为 yyyy-mm-dd 
			ps.executeUpdate(); 
			 
			this.addCart(order,orderId); 
			 
			flag = true; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return flag; 
	} 
	 
	private boolean isOrderIdExist(String orderId) throws SQLException { 
		boolean flag = true; 
		con = new BaseDao().getConn(); 
		String sql = "select count(*) from cart where orderId=? "; 
		ps = con.prepareStatement(sql); 
		ps.setString(1, orderId); 
		rs = ps.executeQuery(); 
		while(rs.next()){ 
			int temp = Integer.parseInt(rs.getString(1)); 
			if(temp==0){ 
				flag=false; 
			} 
		} 
			return flag; 
	} 
	 
	public boolean addCart(OrderEntity order, String orderId){ 
		boolean flag = false; 
		con = new BaseDao().getConn(); 
		String sql = "insert into cart(orderId,mealId,amount) values(?,?,?)"; 
		Map<Integer,MealItem> cart =  order.getCart().getMap(); 
		Iterator<Integer> i = cart.keySet().iterator(); 
		try { 
			ps = con.prepareStatement(sql); 
			while(i.hasNext()){ 
				int key = (Integer) i.next(); 
				MealItem meal = (MealItem) cart.get(key); 
				ps.setString(1, orderId); 
				ps.setInt(2, key); 
				ps.setInt(3, meal.getQuantity()); 
				ps.executeUpdate(); 
			} 
			flag=true; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return flag; 
	} 
	 
	private void close(Connection con1, PreparedStatement ps1, ResultSet rs1) { 
		try { 
			if(con1!=null) 
			con1.close(); 
			if(ps1!=null) 
			ps1.close(); 
			if(rs1!=null) 
			rs1.close(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
	} 
}