package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entity.CommodityType;




/**
 * @author Administrator
 *商品类型的数据库处理类
 * 对应数据库COMMODITYTYPE表
 */
public class CommodityTypeDao {
	DBconn conn = new DBconn();
	//在数据库中添加商品类型
	public boolean add(CommodityType com){
		return conn.update("insert into CommodityType(PARENTTYPEID,TYPENAME) values(?,?)", 
				new String[]{String.valueOf(com.getParentTypeId()),com.getTypeName()})>0;
	} 
	//根据商品id来删除商品类型
	public boolean deleteById(int typeId){
		return conn.update("delete from CommodityType where typeId=?", 
				new String[]{String.valueOf(typeId)})>0;
	}
	//根据商品对象来更新商品类型
	public boolean updateByCommodityType(CommodityType com){
		return conn.update("update CommodityType set PARENTTYPEID=?,TYPENAME=? where typeId=?", 
				new String[]{String.valueOf(com.getParentTypeId()),
				             com.getTypeName(),
				             String.valueOf(com.getTypeId())})>0;
	}
	/**
	 * @param typeId
	 * @return根据ID获取商品类型的数据
	 */
	public CommodityType queryById(int typeId){
		CommodityType com=new CommodityType();
		ResultSet rs=conn.query("select * from CommodityType where typeId=?", new String[]{String.valueOf(typeId)});
		try {
			while(rs.next()){
				com.setTypeId(rs.getInt("typeId"));		//类别编号
				com.setTypeName(rs.getString("typeName"));	//类别名称
				com.setParentTypeId(rs.getInt("parentTypeId"));	//父类ID
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			conn.closeAll();		
		}
		return com;
	}/**
	 * @param typeId
	 * @return根据父类ID获取商品类型的数据
	 */
	public ArrayList<CommodityType> queryByPid(int parentTypeId){
		ArrayList<CommodityType> list=new ArrayList<CommodityType>();
		ResultSet rs=conn.query("select * from CommodityType where parentTypeId=? order by typeId ", new String[]{String.valueOf(parentTypeId)});
		try {
			while(rs.next()){
				CommodityType com=new CommodityType(rs.getInt("typeId"),
                        rs.getString("typeName"),
                        rs.getInt("parentTypeId"));
list.add(com);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			conn.closeAll();		
		}
		return list;
	}
	/**
	 * @return
	 * 返回所有的商品类型数据
	 */
	public ArrayList<CommodityType> queryAll(){
		ArrayList<CommodityType> list=new ArrayList<CommodityType>();
		ResultSet rs=conn.query("select * from CommodityType order by parentTypeId,typeId  ", null);
		try {
			while(rs.next()){
				CommodityType com=new CommodityType(rs.getInt("typeId"),
						                            rs.getString("typeName"),
						                            rs.getInt("parentTypeId"));
				list.add(com);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			conn.closeAll();
		}
		return list;
	}
	
	/**
	 * @param typeName类型名称
	 * @return根据类型名查询类型id
	 */
	public int queryTypeId(String typeName){
		int typeId=-1;
		String sql = "select typeid from commodityType where typename=?";
		ResultSet rs=conn.query(sql, new String[]{typeName});
		try {
			if(rs.next()){
				typeId = rs.getInt("typeid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return typeId;
	}
	
	/**
	 * @return所有子类型
	 */
	public ArrayList<CommodityType> querySubType(){
		ArrayList<CommodityType> list = new ArrayList<CommodityType>();
		String sql = "select * from commodityType where PARENTTYPEID<>0 order by typeId";
		ResultSet rs=conn.query(sql, null);
		try {
			while(rs.next()){
				CommodityType type = new CommodityType();
				type.setTypeId(rs.getInt(1));
				type.setParentTypeId(rs.getInt(2));
				type.setTypeName(rs.getString(3));
				list.add(type);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			conn.closeAll();
		}
		return list;
	}
}
	

最近下载更多
202105013142  LV1 2024年1月29日
komorebi123987  LV5 2023年12月9日
2816681380  LV2 2023年1月30日
wanglinddad  LV55 2022年4月15日
微信网友_5870618216976384  LV6 2022年3月14日
lishangchen  LV3 2022年1月27日
sovy030201  LV5 2022年1月11日
765105637  LV9 2022年1月11日
八七  LV8 2022年1月3日
北有深秋  LV3 2021年12月21日
最近浏览更多
kingwjay  LV2 5月21日
cassie555  LV3 1月16日
dongandmin  LV9 2024年12月31日
appppp  LV1 2024年12月12日
微信网友_7290996505972736  LV4 2024年12月11日
chinajy  LV2 2024年12月8日
Daima000  LV4 2024年12月3日
Rucoding  LV8 2024年11月18日
微信网友_7249860635152384 2024年11月11日
暂无贡献等级
求学的熊猫  LV11 2024年9月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友