首页>代码>java swing开发毕业设计-进销存管理系统源代码下载>/ESMS/src/com/sxt/gmms/dao/base/CustomerDao.java
package com.sxt.gmms.dao.base;

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

import com.sxt.gmms.dao.DBUtil;
import com.sxt.gmms.entity.Customer;

/**
 * 会员DAO层
 * 
 * @author ming
 * 
 */
public class CustomerDao {

	/**
	 * 按拼音码查询
	 * 
	 * @param pym
	 * @return
	 */
	public List<Customer> findCustomerList(String pym) {
		Connection con = null;
		Statement stat = null;
		ResultSet rs = null;
		List<Customer> cusList = new ArrayList<Customer>();
		try {
			con = DBUtil.getConn();
			stat = con.createStatement();
			String sql = "select * from iss_customer where cus_pym like '"
					+ "%" + pym + "%" + "' order by cus_id desc";
			rs = stat.executeQuery(sql);
			while (rs.next()) {
				// 收集信息
				int cusId = rs.getInt("cus_id");
				String cusCode = rs.getString("cus_code");
				String cusName = rs.getString("cus_name");
				String cusPym = rs.getString("cus_pym");
				int cusAge = rs.getInt("cus_age");
				String cusPhone = rs.getString("cus_phone");
				int cusSex = rs.getInt("cus_sex");
				String cusAddre = rs.getString("cus_comaddress");
				int cusStatus = rs.getInt("cus_status");
				String cusComName = rs.getString("cus_comname");
				// 组装
				Customer cus = new Customer(cusId, cusCode, cusName, cusPym,
						cusAge, cusPhone, cusSex, cusComName, cusAddre,
						cusStatus);
				cusList.add(cus);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(con, stat, rs);
		}
		return cusList;
	}

	/**
	 * 修改指定的会员
	 * 
	 * @param cus
	 */
	public void updateCustomer(Customer cus) {
		Connection con = null;
		PreparedStatement stat = null;
		try {
			con = DBUtil.getConn();
			String sql = "update iss_customer set  " + "cus_code = ?, "
					+ "cus_name = ?, " + "cus_pym = ?, " + "cus_age = ?, "
					+ "cus_phone = ?, " + "cus_sex = ?, "+ "cus_comaddress = ?, " 
					+ "cus_status = ?, "+ "cus_comname = ? " 
					+ " where cus_code = '" + cus.getCusCode() + "'";
			stat = con.prepareStatement(sql);
			stat = con.prepareStatement(sql);
			stat.setString(1, cus.getCusCode());
			stat.setString(2, cus.getCusName());
			stat.setString(3, cus.getCusPym());
			stat.setInt(4, cus.getCusAge());
			stat.setString(5, cus.getCusPhone());
			stat.setInt(6, cus.getCusSex());
			stat.setString(7, cus.getCusAddre());
			stat.setInt(8, cus.getCusStatus());
			stat.setString(9, cus.getCusComName());
			stat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(con, stat, null);
		}
	}

	/**
	 * 查找指定会员的信息
	 * 
	 * @param code
	 * @return
	 */
	public Customer findCustomer(String code) {
		Connection con = null;
		Statement stat = null;
		ResultSet rs = null;
		Customer cus = null;
		try {
			con = DBUtil.getConn();
			stat = con.createStatement();
			String sql = "select * from iss_customer where cus_code = '" + code
					+ "'";
			rs = stat.executeQuery(sql);
			if (rs.next()) {
				// 收集信息
				int cusId = rs.getInt("cus_id");
				String cusCode = rs.getString("cus_code");
				String cusName = rs.getString("cus_name");
				String cusPym = rs.getString("cus_pym");
				int cusAge = rs.getInt("cus_age");
				String cusPhone = rs.getString("cus_phone");
				int cusSex = rs.getInt("cus_sex");
				String cusAddre = rs.getString("cus_comaddress");
				int cusStatus = rs.getInt("cus_status");
				String cusComName = rs.getString("cus_comname");
				// 组装
				cus = new Customer(cusId, cusCode, cusName, cusPym, cusAge,
						cusPhone, cusSex, cusComName, cusAddre, cusStatus);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(con, stat, rs);
		}
		return cus;
	}

	/**
	 * 删除指定编号的会员
	 * 
	 * @param cusCode
	 */
	public void delCustomer(String cusCode) {
		String sql = "delete from iss_customer where cus_code = '" + cusCode
				+ "'";
		DBUtil.executeUpdate(sql);
	}

	/**
	 * 添加会员到数据库
	 * 
	 * @param cus
	 */
	public void addCustomer(Customer cus) {
		Connection con = null;
		PreparedStatement stat = null;
		try {
			con = DBUtil.getConn();
			String sql = "insert into iss_customer(" + "cus_code, "
					+ "cus_name, " + "cus_pym, " + "cus_age, " + "cus_phone, "
					+ "cus_sex, " + "cus_comaddress, " + "cus_status,"
					+ "cus_comname) " + " values(?,?,?,?,?,?,?,?,?)";
			stat = con.prepareStatement(sql);
			stat.setString(1, cus.getCusCode());
			stat.setString(2, cus.getCusName());
			stat.setString(3, cus.getCusPym());
			stat.setInt(4, cus.getCusAge());
			stat.setString(5, cus.getCusPhone());
			stat.setInt(6, cus.getCusSex());
			stat.setString(7, cus.getCusAddre());
			stat.setInt(8, cus.getCusStatus());
			stat.setString(9, cus.getCusComName());
			stat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(con, stat, null);
		}
	}

	/**
	 * 把Customer的信息从数据库中取出来
	 * 
	 * @return
	 */
	public List<Customer> loadCustomerList() {
		Connection con = null;
		Statement stat = null;
		ResultSet rs = null;
		List<Customer> cusList = new ArrayList<Customer>();
		try {
			con = DBUtil.getConn();
			stat = con.createStatement();
			String sql = "select * from iss_customer order by cus_id desc";
			rs = stat.executeQuery(sql);
			while (rs.next()) {
				// 收集信息
				int cusId = rs.getInt("cus_id");
				String cusCode = rs.getString("cus_code");
				String cusName = rs.getString("cus_name");
				String cusPym = rs.getString("cus_pym");
				int cusAge = rs.getInt("cus_age");
				String cusPhone = rs.getString("cus_phone");
				int cusSex = rs.getInt("cus_sex");
				String cusAddre = rs.getString("cus_comaddress");
				int cusStatus = rs.getInt("cus_status");
				String cusComName = rs.getString("cus_comname");
				// 组装
				Customer cus = new Customer(cusId, cusCode, cusName, cusPym,
						cusAge, cusPhone, cusSex, cusComName, cusAddre,
						cusStatus);
				cusList.add(cus);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(con, stat, rs);
		}
		return cusList;
	}
}
最近下载更多
ruifeng  LV4 3月3日
LTPR66  LV1 2023年12月1日
1145304128  LV12 2023年5月20日
CL200228  LV4 2023年4月15日
Seem丶君  LV3 2022年11月22日
计算机暴龙战士  LV16 2022年11月19日
srl2881552  LV10 2022年9月5日
youwuzuichen  LV10 2022年8月17日
liuchenru  LV1 2022年6月10日
wanglinddad  LV54 2022年4月6日
最近浏览更多
罗清晨  LV11 2月21日
bridgezn  LV1 2月1日
LTPR66  LV1 2023年11月28日
haotzy  LV3 2023年10月5日
类人孩 2023年9月30日
暂无贡献等级
yhwcoder 2023年7月2日
暂无贡献等级
17683946472  LV9 2023年6月8日
huangzy  LV12 2023年6月7日
陆程江  LV2 2023年5月29日
lichengai  LV7 2023年5月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友