www.pudn.com > zhaobiao.rar > NewProduct.java


/** 
 *  
 */ 
package com.centralsoft.zhaobiao.admin; 
 
import java.sql.*; 
import java.util.*; 
 
import com.centralsoft.zhaobiao.qtdataguanli.*; 
import com.centralsoft.zhaobiao.util.*; 
 
/** 
 * @author zw 
 * 
 */ 
public class NewProduct { 
	public NewProduct() {		 
	} 
	long NewProduct_id; 
	public void setNewProduct_id(long NewProduct_id) { 
		this.NewProduct_id = NewProduct_id; 
	} 
	public long getNewProduct_id() { 
		return NewProduct_id; 
	} 
	/** 
	 * 根据产品id删除该产品,其中在删除该产品时页删除ppm表中与该产品相关的记录 
	 * @param product_id 
	 */ 
	public void delProductid(long product_id) { 
		Connection conn = null; 
		dataBase db = new dataBase(); 
		conn = db.getConnection(); 
		String sql = "delete from product where product_id = ?"; 
		PreparedStatement ps = null; 
		try { 
			ps = conn.prepareStatement(sql); 
			ps.setLong(1,product_id); 
			ps.executeUpdate(); 
			//删除所有的ppm表中的该产品记录 
			NewPPM newppm=new NewPPM(); 
		    newppm.delPPMProductid(product_id); 
		} catch (Exception e) { 
			System.out.println(e.getMessage()); 
		} finally { 
			try { 
				if (ps != null) { 
					ps.close(); 
				} 
				db.closeConn(); 
			} catch (Exception e) { 
				System.out.println(e.getMessage()); 
			} 
		} 
	} 
	/** 
	 * 根据厂商id删除产品表中的有关产品 
	 * @param maker_id 
	 */ 
	public void delProductMakerid(long maker_id) { 
		Connection conn = null; 
		dataBase db = new dataBase(); 
		conn = db.getConnection(); 
		String sql = "select * from product where maker_id = ?"; 
		PreparedStatement ps = null; 
		ResultSet rs = null; 
		try { 
			ps = conn.prepareStatement(sql); 
			ps.setLong(1,maker_id); 
			rs = ps.executeQuery(); 
			while (rs.next()) { 
				delProductid(rs.getLong("product_id")); 
			} 
		} catch (Exception e) { 
			System.out.println(e.getMessage()); 
		} finally { 
			try { 
				if (rs != null) { 
					rs.close(); 
				} 
				if (ps != null) { 
					ps.close(); 
				} 
				db.closeConn(); 
			} catch (Exception e) { 
				System.out.println(e.getMessage()); 
			} 
		} 
	} 
	/** 
	 * 根据厂商id获得属于该厂商的所有所有产品名称和id 
	 * @param maker_id 
	 * @return 
	 */ 
	public Vector getProductMakerid(long maker_id) { 
		Vector list = new Vector(); 
		dataBase db = new dataBase(); 
		ResultSet rs = null; 
		String sql = "select * from product where maker_id ="+maker_id; 
		rs = db.getResultSet(sql); 
		try { 
			while (rs.next()) { 
				Product pd = new Product(); 
				pd.setProduct_id(rs.getLong("product_id")); 
				pd.setProduct_name(rs.getString("product_name")); 
				pd.setMaker_id(rs.getLong("maker_id")); 
				pd.setType_id(rs.getLong("type_id")); 
				pd.setProduct_content(rs.getString("product_content")); 
				list.add(pd); 
			} 
		} catch (Exception e) { 
			System.out.println(e.getMessage()); 
		} finally { 
			try { 
				if (rs != null) { 
					rs.close(); 
				} 
				db.closeConn(); 
			} catch (Exception e) { 
				System.out.println(e.getMessage()); 
			} 
		} 
		return list; 
	} 
	/** 
	 * 添加一种产品 
	 * @param pd 
	 */ 
	public void addProduct(Product pd) { 
		dataBase db = new dataBase(); 
		Connection conn = null; 
		ResultSet rs = null; 
		PreparedStatement ps = null; 
		String addPd = "insert into product (product_id,product_name,maker_id,type_id,product_content) " + 
				"values (?,?,?,?,?)"; 
		long maxid = 0; 
		rs = db.getResultSet("select max(product_id) as maxid from product"); 
		try { 
			if (rs.next()) { 
				maxid = rs.getLong("maxid") + 1; 
			} else { 
				maxid = 1; 
			} 
			setNewProduct_id(maxid); 
		} catch (Exception e) { 
			System.out.println(e.getMessage()); 
		} finally { 
			try { 
				if (rs != null) { 
					rs.close(); 
				} 
			} catch (Exception e) { 
				System.out.println(e.getMessage()); 
			} 
		} 
		try { 
			conn = db.getConnection(); 
			ps = conn.prepareStatement(addPd); 
			ps.setLong(1,maxid); 
			ps.setString(2,pd.getProduct_name()); 
			ps.setLong(3,pd.getMaker_id()); 
			ps.setLong(4,pd.getType_id()); 
			ps.setString(5,pd.getProduct_content()); 
			ps.executeUpdate(); 
		} catch (Exception e) { 
			System.out.println(e.getMessage()); 
		} finally { 
			try { 
				if (ps != null) { 
					ps.close(); 
				} 
				db.closeConn(); 
			} catch (Exception e) { 
				System.out.println(e.getMessage()); 
			} 
		} 
	} 
	/** 
	 * 验证新添加(或删除)的产品id是不是存在于数据库中 
	 * 1代表添加成功,0代表删除成功 
	 * @return 
	 */ 
	public int certificateProduct_id() { 
		int count = 0; 
		long maxid = getNewProduct_id(); 
		dataBase db = new dataBase(); 
		ResultSet rs = null; 
		String sql = "select * from product where product_id = "+maxid; 
		rs = db.getResultSet(sql); 
		try { 
			while (rs.next()) { 
				count ++; 
			} 
			System.out.println("验证新添加(或删除)的产品id是不是存在于数据库中:"+count); 
		} catch (Exception e) { 
			System.out.println(e.getMessage()); 
		} finally { 
			try { 
				if (rs != null) { 
					rs.close(); 
				} 
				db.closeConn(); 
			} catch (Exception e) { 
				System.out.println(e.getMessage()); 
			} 
		} 
		return count; 
	} 
}