前台dao 接口与实现 ProductsBrandDao

package cn.client.dao;

import java.util.List;
import cn.entity.ProductsBrand;
/*
 * 商品品牌表 DAO 接口
 * */
public interface ProductsBrandDao {
	//商品品牌表列表
	public List<ProductsBrand> productsBrandList();

	//根据名称获得对应的值
	public int selectKey(String productsBrandContent);

	//根据名称和分类号获得对应的值
	public int selectKey(String productsBrandContent, int productsClassifyId);

	//根据名称获得对应的分类的值
	public int selectClassifyKey(String productsBrandContent);

	//根据分类号来获取列表
	public List<ProductsBrand> productsBrandList(int productsClassifyId);
}

 

package cn.client.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.client.dao.BaseDao;
import cn.client.dao.ProductsBrandDao;
import cn.entity.ProductsBrand;
/*
 * 商品品牌表 DAO 实现
 * */
public class ProductsBrandDaoImpl extends BaseDao implements ProductsBrandDao {
	Connection conn = null;//数据库连接  
	PreparedStatement pstmt = null;//数据库操作  
	ResultSet rs = null;//保存查询结果  
	public List<ProductsBrand> productsBrandList() {
		List<ProductsBrand> productsBrandList = new ArrayList<ProductsBrand>();
		ProductsBrand item = null;
		String sql = "select products_brand_id, products_brand_content, products_classify_id from products_brand order by products_brand_id asc";
		conn=this.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				item = new ProductsBrand(
						rs.getInt(1),
						rs.getString(2),
						rs.getInt(3)
						);
				productsBrandList.add(item);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		return productsBrandList;
	}

	//根据名称获得对应的值
	public int selectKey(String productsBrandContent){
		String sql = 
				"select products_brand_id, products_brand_content, " +
						" products_classify_id from products_brand" + 
						"  where products_brand_content like ?";
		int key = 0;
		conn=this.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,"%"+ productsBrandContent+"%");
			rs = pstmt.executeQuery();
			if(rs.next()){
				key = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		return key;
	}

	//根据名称获得对应的分类的值
	public int selectClassifyKey(String productsBrandContent){
		String sql = "select  products_classify_id from products_brand" +
						" where products_brand_content like ?";
		int key = 0;
		conn=this.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,"%"+ productsBrandContent+"%");
			rs = pstmt.executeQuery();
			if(rs.next()){
				key = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		return key;
	}

	//根据名称和分类号获得对应的值
	public int selectKey(String productsBrandContent, int productsClassifyId){
		String sql = "select products_brand_id, products_brand_content," +
				" products_classify_id from products_brand" + 
				" where products_brand_content like ?" + 
				" and products_classify_id=?";
		int key = 0;
		conn=this.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,"%"+ productsBrandContent+"%");
			pstmt.setInt(2, productsClassifyId);
			rs = pstmt.executeQuery();
			if(rs.next()){
				key = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		return key;
	}

	//根据分类号来获取列表
	public List<ProductsBrand> productsBrandList(int productsClassifyId){
		List<ProductsBrand> productsBrandList = new ArrayList<ProductsBrand>();
		ProductsBrand item = null;
		String sql ="select products_brand_id, products_brand_content, " +
				" products_classify_id from products_brand " + 
				" where products_classify_id=?";
		conn=this.getConnection();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, productsClassifyId);
			rs = pstmt.executeQuery();
			while(rs.next()){
				item = new ProductsBrand(
						rs.getInt(1),
						rs.getString(2),
						rs.getInt(3)
						);
				productsBrandList.add(item);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		return productsBrandList;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值