⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 100137b3df46001c1160e657ddb92502

📁 是一个网上手机超市
💻
字号:
package hall;
/**
 * Project:    NWPU online shop
 * JDK version used: jdk1.5.0
 * Version: 	1.01
 * class opProduct 用来处理有关于货物的各种操作
 */

import java.sql.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;

public class opProduct {
	
	private Vector products;//存放货物的向量数组

	private javax.servlet.http.HttpServletRequest request;//请求页面

	private products prod;//货物 

	private int page = 1; // 显示的页码

	private int pageSize = 12; // 每页显示的留言数

	private int pageCount = 0; // 页面总数

	private long recordCount = 0; // 查询的记录总数

	private DBWrapper myConnection = null;

	private String sqlStr = "";

	public opProduct() throws Exception {
		myConnection = DBWrapper.Instance();
		prod = new products();
	}

	/**
	 * boolean getRequest(javax.servlet.http.HttpServletRequest newrequest)
	 * Description :分解货物添加和修改页面发来的请求页面,并将信息放入货物实例中
	 * @param HttpServletRequest
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean getRequest(javax.servlet.http.HttpServletRequest newrequest) {

		boolean flag = false;

		try {
			request = newrequest;
			String item = request.getParameter("producItem");
			int productItem = 0;
			try {
				productItem = Integer.parseInt(item);
			} catch (Exception e) {
			}
			prod.setProductorItem(productItem);

			String productname = request.getParameter("producName");
			if (productname == null || productname.equals("")) {
				productname = "";
			}
			prod.setProductorName(productname);

			// productQuantity

			String quantity = request.getParameter("quantity");
			int productquantity = Integer.parseInt(quantity);
			prod.setQuantity(productquantity);

			// picture address
			String address = request.getParameter("address");
			;
			if (address == null) {
				address = "";
			}
			prod.setAddress(address);

			// provideName
			String providename = request.getParameter("provideName");
			if (providename == null) {
				providename = "";
			}
			prod.setProvideName(providename);

			// type
			String type = request.getParameter("type");
			if (type == null) {
				type = "";
			}
			prod.setType(type);

			// price
			float price;
			try {
				price = new Float(request.getParameter("price")).floatValue();
			} catch (Exception e) {
				price = 0;
			}
			prod.setPrice(price);

			// appearance
			String appearance = request.getParameter("appearance");
			if (appearance == null) {
				appearance = "";
			}
			prod.setAppearance(appearance);

			// photograph
			String photograph = request.getParameter("photograph");
			if (photograph == null) {
				photograph = "";
			}
			prod.setPhotograph(photograph);

			// color
			String color = request.getParameter("color");
			if (color == null) {
				color = "";
			}
			prod.setColor(color);

			// system
			String system = request.getParameter("system");
			if (system == null) {
				system = "";
			}
			prod.setSystem(system);

			// save
			String save = request.getParameter("save");
			if (save == null) {
				save = "";
			}
			prod.setSystem(system);

			// ring
			String ring = request.getParameter("ring");
			if (ring == null) {
				ring = "";
			}
			prod.setRing(ring);

			// isAptitude
			String siaAptitude = request.getParameter("isAptitude");
			boolean iaAptitude;
			if (siaAptitude == null) {
				iaAptitude = false;
			} else {
				iaAptitude = Boolean.parseBoolean(siaAptitude);
			}
			prod.setIsAptitude(iaAptitude);

			flag = true;
			return flag;
		} catch (Exception e) {
			return flag;
		}
	}

	/**
	 * boolean insert()
	 * 完成商品添加
	 * @return boolean 返回操作是否成功信息
	 * @throws java.sql.SQLException
	 */
	public boolean insert() throws Exception {
		sqlStr = "insert into products values (";
		sqlStr = sqlStr + prod.getProductorItem() + ",'";
		sqlStr = sqlStr + prod.getProductorName() + "',";
		sqlStr = sqlStr + prod.getQuantity() + ",'";
		sqlStr = sqlStr + prod.getAddress() + "','";
		sqlStr = sqlStr + prod.getProvideName() + "','";
		sqlStr = sqlStr + prod.getType() + "',";
		sqlStr = sqlStr + prod.getPrice() + ",'";
		sqlStr = sqlStr + prod.getAppearance() + "','";
		sqlStr = sqlStr + prod.getPhotograph() + "','";
		sqlStr = sqlStr + prod.getColor() + "','";
		sqlStr = sqlStr + prod.getSystem() + "','";
		sqlStr = sqlStr + prod.getRing() + "','";
		sqlStr = sqlStr + prod.getSave() + "',";
		sqlStr = sqlStr + prod.getIsAptitude() + " )";
		try {
			myConnection.runUpdate(sqlStr);
			return true;
		} catch (SQLException sqle) {
			System.out.print(sqle.getMessage());
			return false;
		}
	}

	/**
	 * 完成商品修改
	 * 
	 * @return boolean 返回操作是否成功信息
	 * @throws java.sql.SQLException
	 */
	public boolean update(HttpServletRequest inRequest)
			throws Exception {
		request = inRequest;
		String sItem = request.getParameter("producItem");
		String sprice = request.getParameter("price");
		String squantity = request.getParameter("quantity");
		int inProdItem = Integer.parseInt(sItem);
		double price;
		int quantity;
		if(sprice == null || sprice == ""){
			sqlStr = "select price from products where producItem = " + inProdItem; 
			try {
				System.out.print(sqlStr);
				ResultSet rs = myConnection.runQuery(sqlStr);
				if(rs.next()){
					price = rs.getDouble(1);
				}
				else
					return false;
			} catch (SQLException e) {
				System.out.print(e.getMessage());
				return false;
			}
		}
		else{
			price = Double.parseDouble(sprice);
		}
		if(squantity == null || squantity ==""){
			quantity = 0;
		}else{
			
			quantity = Integer.parseInt(squantity);
		}
		sqlStr = "update products set ";
		sqlStr = sqlStr + "price = " + price + " , ";
		sqlStr = sqlStr + "quantity = quantity + " + quantity + " , ";
		sqlStr = sqlStr + "where producItem = " + inProdItem;
		try {
			System.out.print(sqlStr);
			myConnection.runUpdate(sqlStr);
			return true;
		} catch (SQLException e) {
			System.out.print(e.getMessage());
			return false;
		}

	}

	/**
	 * boolean delete(int aid)
	 * 完成商品删除
	 * 
	 * @param int 货物号 
	 * @return boolean 返回操作是否成功信息
	 * @throws java.sql.SQLException
	 */
	public boolean delete(int aid) throws Exception {
		sqlStr = "delete from products where producItem = " + aid;
		try {
			System.out.print(sqlStr);
			myConnection.runUpdate(sqlStr);
			return true;
		} catch (SQLException e) {
			System.out.println(e);
			return false;
		}
	}

	/**
	 * product_search(String inProdName, String inType,
	 *		String saccount1, String saccount2)
	 * 完成对商品的搜索,可以按名字关键字,型号,价格范围等查找,也可以结合查找
	 * 不需要用到的参数用“”代替。查询到的货物,需要的页的信息存入Vector中供取用
	 * 
	 * @param String 货物名关键字
	 * @param String 型号
	 * @param String 价格范围下限
	 * @param String 价格范围上限 
	 * @return boolean 返回操作是否成功信息
	 * @throws java.sql.SQLException
	 */
	public boolean product_search(String inProdName, String inType,
			String saccount1, String saccount2) throws Exception {

		String type = inType;
		String prodName = inProdName;

		double account1;
		double account2;
		//若下限没输入,默认为0,上限没输入默认为100000
		if (saccount1 == "" || saccount1 == null) {
			account1 = 0;
		} else {
			account1 = Double.parseDouble(saccount1);
		}
		if (saccount2 == "" || saccount2 == null) {
			account2 = 100000;
		} else {
			account2 = Double.parseDouble(saccount2);
		}
		if (inType == null)
			type = "";
		if (inProdName == null)
			prodName = "";

		String sqlStr1 = "", sqlStr2 = "";
		if (!type.equals("other")) {
			sqlStr1 = "select count(*) from products where producName like '%"
					+ prodName + "%'" + " and type like '%" + type
					+ "%' and (price between " + account1 + " and " + account2
					+ " )";
			sqlStr2 = "select * from products where producName like '%"
					+ prodName + "%' and type like '%" + type
					+ "%' and (price between " + account1 + " and " + account2
					+ " ) order by producItem";
		} else {
			sqlStr1 = "select count(*) from products where producName like '%"
					+ prodName + "%'"
					+ " and type not in ('三星','诺基亚','索爱','摩托罗拉','西门子')"
					+ " and (price between " + account1 + " and " + account2
					+ " )";
			sqlStr2 = "select * from products where producName like '%"
					+ prodName
					+ "%' and type not in ('三星','诺基亚','索爱','摩托罗拉','西门子')"
					+ " and (price between " + account1 + " and " + account2
					+ " ) order by producItem";
		}

		try {
			ResultSet rs1 = myConnection.runQuery(sqlStr1);
			if (rs1.next())
				recordCount = rs1.getInt(1);
			rs1.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return false;
		}

		// 设定有多少pageCount
		if (recordCount < 1)
			pageCount = 0;
		else
			pageCount = (int) (recordCount - 1) / pageSize + 1;
		// 检查查看的页面数是否在范围内
		if (page < 1)
			page = 1;
		else if (page > pageCount)
			page = pageCount;

		try {
			ResultSet rs = myConnection.runQuery(sqlStr2);
			products = new Vector();
			if (page == 1) {

			} else {
				for (int i = 0; i < pageSize * (page - 1); i++) {
					rs.next();
				}
			}
			for (int i = 0; i < pageSize; i++) {
				if (rs.next()) {
					products pr = new products();
					pr.setProductorItem(rs.getInt("producItem"));
					pr.setProductorName(rs.getString("producName"));
					pr.setAddress(rs.getString("address"));
					pr.setAppearance(rs.getString("appearance"));
					pr.setColor(rs.getString("color"));
					pr.setIsAptitude(rs.getBoolean("iaAptitude"));
					pr.setPhotograph(rs.getString("photograph"));
					pr.setPrice(rs.getDouble("price"));
					pr.setProvideName(rs.getString("provideName"));
					pr.setQuantity(rs.getInt("quantity"));
					pr.setRing(rs.getString("ring"));
					pr.setSave(rs.getString("save"));
					pr.setSystem(rs.getString("system"));
					pr.setType(rs.getString("type"));
					products.addElement(pr);
				} else {
					break;
				}

			}
			rs.close();
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}

	/**
	 * boolean showProduct(int inItem)
	 * 找到指定货物信息,并将货物存入Vector 中供取用
	 * @param int 货物号
	 * @return boolean 返回操作是否成功信息
	 * @throws java.sql.SQLException
	 */
	public boolean showProduct(int inItem) {
		boolean flag = false;
		sqlStr = "select * from products where producItem =" + inItem + " ";
		try {
			ResultSet rs = myConnection.runQuery(sqlStr);
			products = new Vector();
			if (rs.next()) {
				products pr = new products();
				pr.setProductorItem(rs.getInt("producItem"));
				pr.setProductorName(rs.getString("producName"));
				pr.setAddress(rs.getString("address"));
				pr.setAppearance(rs.getString("appearance"));
				pr.setColor(rs.getString("color"));
				pr.setIsAptitude(rs.getBoolean("isAptitude"));
				pr.setPhotograph(rs.getString("photograph"));
				pr.setPrice(rs.getDouble("price"));
				pr.setProvideName(rs.getString("provideName"));
				pr.setQuantity(rs.getInt("quantity"));
				pr.setRing(rs.getString("ring"));
				pr.setSave(rs.getString("save"));
				pr.setSystem(rs.getString("system"));
				pr.setType(rs.getString("type"));

				products.addElement(pr);
				flag = true;
			} else {
				flag = false;
			}
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return flag;
	}

	/**
	 * int getPage()
     * Description :得到要显示的页数
     * @return int
     */
	public int getPage() { 
		return page;
	}

	/**
	 * void setPage(int newpage)
     * Description :修改要显示的页数
     * @param int
     */
	public void setPage(int newpage) {
		page = newpage;
	}

	/**
	 * int getPageSize()
     * Description :得到每页要显示的商品数
     * @return int
     */
	public int getPageSize() { // 每页显示的商品数
		return pageSize;
	}

	/**
	 * void setPageSize(int newpsize)
     * Description :修改每页要显示的商品数
     * @param int
     */
	public void setPageSize(int newpsize) {
		pageSize = newpsize;
	}

	/**
	 * int getPageCount()
     * Description :得到页面总数
     * @return int
     */
	public int getPageCount() { // 页面总数
		return pageCount;
	}

	/**
	 * void setPageCount(int newpcount)
     * Description :修改页面总数
     * @param int
     */
	public void setPageCount(int newpcount) {
		pageCount = newpcount;
	}

	/**
	 * int getRecordCount()
     * Description :得到记录总数
     * @return long
     */
	public long getRecordCount() {
		return recordCount;
	}

	/**
	 * void setRecordCount(long newrcount)
     * Description :修改记录总数
     * @param long
     */
	public void setRecordCount(long newrcount) {
		recordCount = newrcount;
	}

	/**
	 * Vector getProducts() 
     * Description :得到存储商品的Vector 
     * @return Vector
     */
	public Vector getProducts() {
		return products;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -