电子拍卖系统开发第五天

今天要将整个项目完结,主要实现功能:

五、浏览拍卖物品:
1、先选择一个物品种类,查看当前种类下的物品,点击该物品,可以进行竞拍物品

六、查看自己的竞标
1、查看自己的竞标记录。

遇到的问题,及其解决方法:

遇到的问题:
Subquery returns more than 1 row查询结果多于一行
解决方法:
原来sql:select * from kind where kind_id =(select kind_id from item where owner_id=?)
改为sql:select * from kind where kind_id = any(select kind_id from item where owner_id=?)

报错:
Duplicate entry '1-567' for key 'item_id' Query: insert into bid(user_id,item_id,bid_price,bid_date) values(?,?,?,?) Parameters: [2, 1, 567.0, Sun Jan 24 12:33:59 GMT+08:00 2016]
Duplicate entry '265' for key 'bid_price' Query: insert into bid(user_id,item_id,bid_price,bid_date) values(?,?,?,?) Parameters: [2, 1, 265.0, Sun Jan 24 12:49:54 GMT+08:00 2016]

从提示的错误代码看,应该是属性值为1的item_id与另一个item_id重复了。通过查看bid表的设计结构,发现这是因为将item_id 、bid_price字段设置为唯一了。

因为item_id字段在item表中是主键自增长 bid_price在bid表中是普通字段,
但是在bid表中 将item_id和bid_price字段设为为唯一,unique(item_id , bid_price),
由于我们用户在参与竞价的时候,要将竞价的记录保存在bid表中,所以竞价的价格和竞价物品的id不能设置为唯一

这个方法虽然能解决 但是不适合该项目的需求,改了数据库设计 就相当于整体改了 不推荐

出现异常:
 Duplicate entry '1-456' for key 'item_id' Query
解决方法:
数据库 bid中 已存在 item_id为1的数据了,你可以先
delete from bid where item_id = 1
然后再 执行添加语句

我现在把项目中重要的代码全部贴出来:

服务端:

BussinessService.java

package com.xbmu.service;

import java.util.List;
import com.xbmu.bean.Bid;
import com.xbmu.bean.Item;
import com.xbmu.bean.Kind;
import com.xbmu.business.BidBean;
import com.xbmu.business.ItemBean;
import com.xbmu.business.KindBean;
import com.xbmu.exception.AuctionException;
/**
 * 业务层接口(主要声明一些主要的功能)
 * @author Administrator
 *
 */
public interface BussinessService{
	/**
	 * 根据赢取者查询物品
	 * @param winerId 赢取者的ID
	 * @return 赢取者获得的全部物品
	 */
	List<ItemBean> getItemByWiner(Integer winerId)
		throws AuctionException;

	/**
	 * 查询流拍的全部物品
	 * @return 全部流拍物品
	 */
	List<ItemBean> getFailItems()throws AuctionException;

	/**
	 * 根据用户名,密码验证登录是否成功
	 * @param username 登录的用户名
 	 * @param pass 登录的密码
	 * @return 登录成功返回用户ID,否则返回-1
	 */
	int validLogin(String username , String pass)
		throws AuctionException;

	/**
	 * 查询用户的全部出价
	 * @param userId 竞价用户的ID
	 * @return 用户的全部出价
	 */
	List<BidBean> getBidByUser(Integer userId)
		throws AuctionException;

	/**
	 * 根据用户查找目前仍在拍卖中的全部物品
	 * @param userId 所属者的ID
	 * @return 属于当前用户的、处于拍卖中的全部物品。
	 */
	List<ItemBean> getItemsByOwner(Integer userId)
		throws AuctionException;

	/**
	 * 查询全部种类
	 * @return 系统中全部全部种类
	 */
	List<KindBean> getAllKind() throws AuctionException;

	/**
	 * 添加物品
	 * @param item 新增的物品
	 * @param avail 有效天数
	 * @param kindId 物品种类ID
	 * @param userId 添加者的ID
	 * @return 新增物品的主键
	 */
	int addItem(Item item, int avail , int kindId , Integer userId)
		throws AuctionException;

	/**
	 * 添加种类
	 * @param kind 新增的种类
	 * @return 新增种类的主键
	 */
	int addKind(Kind kind) throws AuctionException;

	/**
	 * 根据产品分类,获取处于拍卖中的全部物品
	 * @param kindId 种类id;
	 * @return 该类的全部产品
	 */
	List<ItemBean> getItemsByKind(int kindId) throws AuctionException;

	/**
	 * 根据种类id获取种类名
	 * @param kindId 种类id;
	 * @return 该种类的名称
	 */
	String getKind(int kindId) throws AuctionException;

	/**
	 * 根据物品id,获取物品
	 * @param itemId 物品id;
	 * @return 指定id对应的物品
	 */
	ItemBean getItem(int itemId) throws AuctionException;

	/**
	 * 增加新的竞价,并对竞价用户发邮件通知
	 * @param itemId 物品id;
	 * @param bid 竞价
	 * @param userId 竞价用户的ID
	 * @return 返回新增竞价记录的ID
	 */
	int addBid(int itemId , Bid bidPrice ,Integer userId)
		throws AuctionException;

	/**
	 * 根据时间来修改物品的赢取者
	 */
	void updateWiner()throws AuctionException;
}

BussinessServiceImpl.java

package com.xbmu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import com.xbmu.bean.Bid;
import com.xbmu.bean.Item;
import com.xbmu.bean.Kind;
import com.xbmu.bean.User;
import com.xbmu.business.BidBean;
import com.xbmu.business.ItemBean;
import com.xbmu.business.KindBean;
import com.xbmu.dao.AuctionUserDao;
import com.xbmu.dao.BidDao;
import com.xbmu.dao.ItemDao;
import com.xbmu.dao.KindDao;
import com.xbmu.dao.StateDao;
import com.xbmu.dao.impl.AuctionUserDaoImpl;
import com.xbmu.dao.impl.BidDaoImpl;
import com.xbmu.dao.impl.ItemDaoImpl;
import com.xbmu.dao.impl.KindDaoImpl;
import com.xbmu.dao.impl.StateDaoImpl;
import com.xbmu.exception.AuctionException;
import com.xbmu.service.BussinessService;

public class BussinessServiceImpl implements BussinessService {
	// ---------------------用户----------------------------
	private AuctionUserDao userDao = new AuctionUserDaoImpl();

	/**
	 * 根据用户名,密码验证登录是否成功
	 * 
	 * @param username
	 *            登录的用户名
	 * @param pass
	 *            登录的密码
	 * @return 登录成功返回用户ID,否则返回-1
	 */
	public int validLogin(String username, String pass) throws AuctionException {
		User user = userDao.findUserByNameAndPass(username, pass);
		if (user != null) {
			return user.getUser_id();
		}
		return -1;
	}

	// ---------------------物品----------------------------
	private ItemDao itemDao = new ItemDaoImpl();

	/**
	 * 查询流拍的全部物品
	 * 
	 * @return 全部流拍物品
	 */
	public List<ItemBean> getFailItems() throws AuctionException {
		try {
			List<Item> items = itemDao.findItemByState(3);
			List<ItemBean> result = new ArrayList<ItemBean>();
			for (Iterator<Item> it = items.iterator(); it.hasNext();) {
				ItemBean ib = new ItemBean();
				initItem(ib, it.next());
				result.add(ib);
			}
			return result;
		} catch (Exception e) {
			throw new AuctionException("查询流拍物品出现异常,请重试");
		}
	}

	// --------------------种类-------------------------
	private KindDao kindDao = new KindDaoImpl();

	/**
	 * 查询全部种类
	 * 
	 * @return 系统中全部种类
	 */
	public List<KindBean> getAllKind() throws AuctionException {
		try {
			List<KindBean> result = new ArrayList<KindBean>();
			List<Kind> kindList = kindDao.findAll();
			for (Kind kind : kindList) {
				result.add(new KindBean(kind.getKind_id(), kind.getKind_name(),
						kind.getKind_desc()));
			}
			return result;
		} catch (Exception e) {
			throw new AuctionException("查询全部种类出现异常,请重试");
		}
	}
	/**
	 * 添加种类
	 * @param kind新增的种类
	 * @return 新增种类的主键
	 */
	public int addKind(Kind kind) throws AuctionException {
		try {
				kindDao.save(kind);
				return kind.getKind_id();
		} catch (AuctionException e) {
			throw new AuctionException("添加种类出现异常,请重试");
		}
	}
	//--------------管理拍卖物品-----------------------
	private StateDao stateDao = new StateDaoImpl();
	//查找自己拍卖的物品
	public List<ItemBean> getItemsByOwner(Integer userId)
			throws AuctionException {
		try {
			List<ItemBean> result = new ArrayList<ItemBean>();
			List<Item> items = itemDao.findItemByOwner(userId);
			for(Iterator<Item> it = items.iterator();it.hasNext();){
				ItemBean ib = new ItemBean();
				initItem(ib, it.next());
				result.add(ib);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("查询用户所有的物品出现异常,请重新");
		}
	}
	//添加拍卖物品
	public int addItem(Item item, int avail, int kindId, Integer userId)
			throws AuctionException {
		//根据种类id获取种类
		Kind kind = kindDao.get(kindId);
		//根据添加着id获取添加者
		User owner = userDao.get(userId);
		//设置Item(物品)属性
		item.setAddtime(new Date());
		//创建Calendar(日历)对象
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, avail);
		item.setEndtime(c.getTime());
		item.setMax_price(item.getInit_price());
		item.setItemState(stateDao.get(1));
		item.setKind(kind);
		item.setOwner(owner);
		//保存item对象
		itemDao.save(item);
		return item.getItem_id();
	}
	//---------------选择物品种类--------------
	public List<ItemBean> getItemsByKind(int kindId) throws AuctionException {
		List<ItemBean> result = new ArrayList<ItemBean>();
		try {
			List<Item> items = itemDao.findItemByKind(kindId);
			for(Iterator<Item> it = items.iterator();it.hasNext();){
				ItemBean ib = new ItemBean();
				initItem(ib,it.next());
				result.add(ib);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw new AuctionException("根据种类获取物品出现异常,请重试!");
		}
	}
	public String getKind(int kindId) throws AuctionException {
		try {
			Kind kind = kindDao.get(kindId);
			if(kind!=null){
				return kind.getKind_name();				
			}
			return null;
		} catch (Exception e) {
			throw new AuctionException("根据种类id获取种类名称出现异常,请重试");
		}
	}
	//--------------------竞价---------------------
	private BidDao bidDao = new BidDaoImpl();
	public int addBid(int itemId, Bid bid, Integer userId)
			throws AuctionException {
		try {
			User user = userDao.get(userId);
			Item item = itemDao.get(itemId);
			if(bid.getBid_price() > item.getMax_price()){
				item.setMax_price(bid.getBid_price());
				itemDao.update(item);
			}
			//设置Bid对象的属性
			bid.setItem(item);
			bid.setUser(user);
			bid.setBid_date(new Date());
			//保存bid对象
			bidDao.save(bid);
			//TODO:准备发送邮件
			return bid.getBid_id();
		} catch (Exception e) {
			e.printStackTrace();
			throw new AuctionException("处理用户竞价出现异常,请重试");
		}
	}
	public ItemBean getItem(int itemId) throws AuctionException {
		try {
			Item item = itemDao.get(itemId);
			ItemBean ib = new ItemBean();
			initItem(ib, item);
			return ib;
		} catch (Exception e) {
			throw new AuctionException("根据物品id获取物品详细信息出现异常,请重试");
		}
	}

	//---------------竞标-------------------
	public List<BidBean> getBidByUser(Integer userId) throws AuctionException {
		try {
			List<Bid> bids = bidDao.findByUser(userId);
			List<BidBean> result = new ArrayList<BidBean>();
			for (int i = 0; i < bids.size(); i++) {
				Bid bid  = bids.get(i);
				BidBean bb = new BidBean();
				initBid(bb, bid);
				result.add(bb);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw new AuctionException("浏览用户的全部竞价出现异常,请重试!");
		}
	}

	/**
	 * 将一个Bid对象装换成BidBean对象
	 * @param bb BidBean对象
	 * @param bid Bid对象
	 */
	private void initBid(BidBean bb, Bid bid) {
		bb.setId(bid.getBid_id().intValue());
		if(bid.getUser()!=null){
			bb.setUser(bid.getUser().getUsername());
		}
		if(bid.getItem()!=null){
			bb.setItem(bid.getItem().getItem_name());
		}
		bb.setPrice(bid.getBid_price());
		bb.setBidDate(bid.getBid_date());
	}
	//查看竞得物品
	public List<ItemBean> getItemByWiner(Integer winerId)
			throws AuctionException {
		try {
			List<ItemBean> result = new ArrayList<ItemBean>();
			List<Item> items = itemDao.findItemByWiner(winerId);
			for(Iterator<Item> it = items.iterator();it.hasNext();){
				ItemBean ib = new ItemBean();
				initItem(ib, it.next());
				result.add(ib);
			}
			return result;
		} catch (Exception e) {
			throw new AuctionException("查询用户所赢取的物品出现异常,请重试");
		}		
	}
	//根据时间来修改物品的状态,赢取者
	public void updateWiner() throws AuctionException {
		try {
			List<Item> itemList = itemDao.findItemByState(1);
			for (int i = 0; i < itemList.size(); i++) {
				Item item = itemList.get(i);
				if(!item.getEndtime().after(new Date())){
					//测试此日期是否在指定日期之后。
					
					//根据指定物品和最高竞价来查询用户
					User user = bidDao.findUserByItemAndPrice(item.getItem_id(),item.getMax_price());
					//如果该物品的最高竞价者不为null
					if(user != null){
						//将该竞价者设为赢取者
						item.setWiner(user);
						//修改物品的状态成为"被赢取"
						item.setItemState(stateDao.get(2));
						itemDao.save(item);
					}else{
						//设置该物品的状态为"流拍"
						item.setItemState(stateDao.get(3));
						itemDao.save(item);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new AuctionException("根据时间来修改物品的状态、赢取者出现异常,请重试");
		}

	}
	/**
	 * 将一个Item PO转换成ItemBean的VO
	 * @param ib  ItemBean的VO
	 * @param item Item的PO
	 */
	private void initItem(ItemBean ib, Item item) {
		ib.setId(item.getItem_id());
		ib.setName(item.getItem_name());
		ib.setDesc(item.getItem_desc());
		ib.setRemark(item.getItem_remark());
		if (item.getKind() != null)
			ib.setKind(item.getKind().getKind_name());
		if (item.getOwner() != null)
			ib.setOwner(item.getOwner().getUsername());
		if (item.getWiner() != null)
			ib.setWiner(item.getWiner().getUsername());
		ib.setAddTime(item.getAddtime());
		ib.setEndTime(item.getEndtime());
		if (item.getItemState() != null)
			ib.setState(item.getItemState().getState_name());
		ib.setInitPrice(item.getInit_price());
		ib.setMaxPrice(item.getMax_price());
	}

}

KindDaoImpl.java

package com.xbmu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.xbmu.bean.Kind;
import com.xbmu.dao.KindDao;
import com.xbmu.util.DBCPUtil;

public class KindDaoImpl implements KindDao {
	QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
	public List<Kind> findAll() {		
		try {
			String sql = "select * from kind";
			List<Kind> kinds = qr.query(sql, new BeanListHandler<Kind>(Kind.class));
			return kinds;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 添加种类:
	 * 遇到问题:需求,想将添加的新数据的id返回,
	 * 使用insert into kind (kind_name,kind_desc) values(?,?);select @@identity as kind_id;语句
	 * 
	 */
	public void save(Kind kind) {
		try {
			String sql = "insert into kind (kind_name,kind_desc) values(?,?)";
			qr.update(sql, kind.getKind_name(),kind.getKind_desc());
			String sql2 = "select @@identity as kind_id";
			//ScalarHandler 的参数为空或null时,返回第一行第一列的数据 
			//ScalarHandler 的参数可以是列的索引(从1开始)或列名 
			Object kind_id = qr.query(sql2, new ScalarHandler(1));
			//因为kind_id在数据库中定义为int型,上面语句返回后为BigInteger型,
			kind.setKind_id(Integer.valueOf(kind_id.toString()));//容易发生类型转换异常
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public Kind get(int kindId) {
		try {
			String sql = "select * from kind where kind_id=?";
			Kind kind = qr.query(sql, new BeanHandler<Kind>(Kind.class), kindId);
			return kind;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
ItemDaoImpl.java
package com.xbmu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.xbmu.bean.Item;
import com.xbmu.bean.Kind;
import com.xbmu.bean.State;
import com.xbmu.bean.User;
import com.xbmu.dao.ItemDao;
import com.xbmu.util.DBCPUtil;

public class ItemDaoImpl implements ItemDao {
	QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());

	/**
	 * 根据物品状态查找物品
	 * 
	 * @param stateId
	 *            状态id;
	 * @return 该状态下的全部物品
	 */
	public List<Item> findItemByState(Integer stateId) {
		try {
			String sql = "select * from item where state_id = ?";
			List<Item> itemList = qr.query(sql, new BeanListHandler<Item>(
					Item.class), stateId);

			// 遍历物品,设置种类,赢取者,拥有者的属性。
			for (Item item : itemList) {
				// 设置种类
				String kindSql = "select * from kind where kind_id = (select kind_id from item where state_id=?)";
				Kind kind = qr.query(kindSql,
						new BeanHandler<Kind>(Kind.class), stateId);
				item.setKind(kind);

				// 设置拥有者用户
				String ownerSql = "select * from auction_user where user_id = (select owner_id from item where state_id=?)";
				User owner = qr.query(ownerSql, new BeanHandler<User>(
						User.class), stateId);
				item.setOwner(owner);

				// 设置赢取者用户
				String winerSql = "select * from auction_user where user_id = (select winer_id from item where state_id=?)";
				User winer = qr.query(winerSql, new BeanHandler<User>(
						User.class), stateId);
				item.setWiner(winer);
			}
			return itemList;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 根据所有者查找处于拍卖中的物品
	 * 
	 * @param userId
	 *            所有者id;
	 * @return 指定用户处于拍卖中的全部物品
	 */
	public List<Item> findItemByOwner(Integer userId) {
		try {
			String sql = "select * from item where owner_id=? and state_id=1";
			List<Item> items = qr.query(sql, new BeanListHandler<Item>(
					Item.class), userId);
			// 设置物品拥有者,物品状态,物品种类
			for (Item item : items) {
				// 设置拥有者
				String ownerSql = "select * from auction_user where user_id=any(select owner_id from item where owner_id=? and state_id=1)";
				User owner = qr.query(ownerSql, new BeanHandler<User>(
						User.class), userId);
				item.setOwner(owner);
				// 设置物品状态
				String stateSql = "select * from state where state_id=1";
				State state = qr.query(stateSql, new BeanHandler<State>(
						State.class));
				item.setItemState(state);
				// 设置物品种类
				String kindSql = "select * from kind where kind_id = any(select kind_id from item where owner_id=?)";
				Kind kind = qr.query(kindSql,
						new BeanHandler<Kind>(Kind.class), userId);
				item.setKind(kind);
			}
			return items;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void save(Item item) {
		try {
			String sql = "insert into item(item_name,item_desc,item_remark,init_price,max_price,kind_id,addtime,endtime,owner_id,state_id) "
					+ //
					"values(?,?,?,?,?,?,?,?,?,?)";
			qr.update(sql, item.getItem_name(), item.getItem_desc(), item
					.getItem_remark(), item.getInit_price(), item
					.getMax_price(), item.getKind().getKind_id(), item
					.getAddtime(), item.getEndtime(), item.getOwner()
					.getUser_id(), item.getItemState().getState_id());
			String sql2 = "select @@identity as item_id";
			Object item_id = qr.query(sql2, new ScalarHandler(1));
			item.setItem_id(Integer.valueOf(item_id.toString()));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public List<Item> findItemByKind(Integer kindId) {
		try {
			// 根据种类浏览拍卖中的物品
			String sql = "select * from item where kind_id=? and state_id=1";
			List<Item> items = qr.query(sql, new BeanListHandler<Item>(
					Item.class), kindId);
			// 设置竞拍物品的状态和拥有者
			for (Item item : items) {
				// 设置状态
				String stateSql = "select * from state where state_id=1";
				State state = qr.query(stateSql, new BeanHandler<State>(
						State.class));
				item.setItemState(state);
				// 设置拥有者
				String ownerSql = "select * from auction_user where user_id = any(select owner_id from item where kind_id=? and item_id=?)";
				User owner = qr.query(ownerSql, new BeanHandler<User>(
						User.class), kindId, item.getItem_id());
				item.setOwner(owner);
			}
			return items;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Item get(int itemId) {
		try {
			String sql = "select * from item where item_id=?";
			Item item = qr
					.query(sql, new BeanHandler<Item>(Item.class), itemId);
			// 设置物品种类
			String kindSql = "select * from kind where kind_id = any(select kind_id from item where item_id=?)";
			Kind kind = qr.query(kindSql, new BeanHandler<Kind>(Kind.class),
					itemId);
			item.setKind(kind);
			// 设置状态
			String stateSql = "select * from state where state_id=1";
			State state = qr.query(stateSql,
					new BeanHandler<State>(State.class));
			item.setItemState(state);
			// 设置拥有者
			String ownerSql = "select * from auction_user where user_id = any(select owner_id from item where item_id=?)";
			User owner = qr.query(ownerSql, new BeanHandler<User>(User.class),
					item.getItem_id());
			item.setOwner(owner);
			return item;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void update(Item item) {
		try {
			String sql = "update item set max_price=? where item_id=?";
			qr.update(sql, item.getMax_price(),item.getItem_id());
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public List<Item> findItemByWiner(Integer userId) {
		try {
			//2:表示拍卖成功
			String sql = "select * from item where winer_id=? and state_id=2";
			List<Item> items = qr.query(sql, new BeanListHandler<Item>(Item.class), userId);
			return items;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

}
BidDaoImpl.java
package com.xbmu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.xbmu.bean.Bid;
import com.xbmu.bean.Item;
import com.xbmu.bean.User;
import com.xbmu.dao.BidDao;
import com.xbmu.util.DBCPUtil;

public class BidDaoImpl implements BidDao {
	private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
	public void save(Bid bid) {
		try {
			//添加竞拍记录之前,先删除先前存在的物品竞价记录
			String deleteSql = "delete from bid where item_id=?";
			qr.update(deleteSql, bid.getItem().getItem_id());
			
			String sql = "insert into bid(user_id,item_id,bid_price,bid_date) values(?,?,?,?)";
			qr.update(sql, bid.getUser().getUser_id(),bid.getItem().getItem_id(),bid.getBid_price(),bid.getBid_date());
			String sql2 = "select @@identity as bid_id";
			Object bid_id = qr.query(sql2, new ScalarHandler(1));
			bid.setBid_id(Integer.valueOf(bid_id.toString()));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public List<Bid> findByUser(Integer userId) {
		try {
			String sql = "select * from bid where user_id=?";
			List<Bid> bids = qr.query(sql, new BeanListHandler<Bid>(Bid.class), userId);
			//设置Bid属性 竞标人 竞标物品
			for (Bid bid : bids) {
				//设置竞标人
				String userSql = "select * from auction_user where user_id=?";
				User user = qr.query(userSql, new BeanHandler<User>(User.class), userId);
				bid.setUser(user);
				//设置竞标物品
				String itemSql = "select * from item where item_id = any (select item_id from bid where user_id=?)";
				Item item = qr.query(itemSql, new BeanHandler<Item>(Item.class), userId);
				bid.setItem(item);
			}
			return bids;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public User findUserByItemAndPrice(Integer item_id, double max_price) {
		try {
			String sql = "select * from bid where item_id=? and bid_price=?";
			List<Bid> bids = qr.query(sql, new BeanListHandler<Bid>(Bid.class), item_id,max_price);
			//返回查询得到的第一个Bid对象关联的AuctionUser对象
			if(bids.size() >= 1){
				Bid b = bids.get(0);
				return b.getUser();
			}
			return null;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
StateDaoImpl.java

package com.xbmu.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.xbmu.bean.State;
import com.xbmu.dao.StateDao;
import com.xbmu.util.DBCPUtil;

public class StateDaoImpl implements StateDao {
	private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
	public State get(int state_id) {
		try {
			// 1表示拍卖中
			String sql = "select * from state where state_id=1";
			State state = qr.query(sql, new BeanHandler<State>(State.class));
			return state;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
AddBidServlet.java
package com.xbmu.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xbmu.bean.Bid;
import com.xbmu.service.BussinessService;
import com.xbmu.service.impl.BussinessServiceImpl;
/**
 * 参与竞价
 * @author Administrator
 *
 */
@WebServlet(urlPatterns="/android/addBid.jsp")
public class AddBidServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String mode = request.getParameter("mode");
		String op = request.getParameter("op");
		//获取userId
		Integer userId = (Integer) request.getSession(true).getAttribute("userId");
		//获取请求参数
		String itemId = request.getParameter("itemId");
		String bidPrice = request.getParameter("bidPrice");
		
		BussinessService service = new BussinessServiceImpl();
		Bid bid = new Bid(Double.parseDouble(bidPrice));
		int bidId = service.addBid(Integer.parseInt(itemId),bid , userId);
		if("android".equals(mode)){
			if(bidId>0){
				response.getWriter().println("恭喜您,竞价成功!");
			}else{
				response.getWriter().println("对不起,竞价失败!");
			}
		}else if("web".equals(mode)){
			if("showAddBidItemUI".equals(op)){
				showAddBidItemUI(request,response);
			}
		}
	}
	/**
	 * 显示竞价页面
	 * @param request
	 * @param response
	 */
	private void showAddBidItemUI(HttpServletRequest request,
			HttpServletResponse response) {
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

客户端与服务端全部代码:http://download.csdn.net/detail/btt2013/9416634



你可能感兴趣的:(电子拍卖系统)