?? 100137b3df46001c1160e657ddb92502
字號(hào):
package hall;
/**
* Project: NWPU online shop
* JDK version used: jdk1.5.0
* Version: 1.01
* class opProduct 用來處理有關(guān)于貨物的各種操作
*/
import java.sql.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
public class opProduct {
private Vector products;//存放貨物的向量數(shù)組
private javax.servlet.http.HttpServletRequest request;//請(qǐng)求頁面
private products prod;//貨物
private int page = 1; // 顯示的頁碼
private int pageSize = 12; // 每頁顯示的留言數(shù)
private int pageCount = 0; // 頁面總數(shù)
private long recordCount = 0; // 查詢的記錄總數(shù)
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 :分解貨物添加和修改頁面發(fā)來的請(qǐng)求頁面,并將信息放入貨物實(shí)例中
* @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 貨物號(hào)
* @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)
* 完成對(duì)商品的搜索,可以按名字關(guān)鍵字,型號(hào),價(jià)格范圍等查找,也可以結(jié)合查找
* 不需要用到的參數(shù)用“”代替。查詢到的貨物,需要的頁的信息存入Vector中供取用
*
* @param String 貨物名關(guān)鍵字
* @param String 型號(hào)
* @param String 價(jià)格范圍下限
* @param String 價(jià)格范圍上限
* @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;
//若下限沒輸入,默認(rèn)為0,上限沒輸入默認(rèn)為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;
}
// 設(shè)定有多少pageCount
if (recordCount < 1)
pageCount = 0;
else
pageCount = (int) (recordCount - 1) / pageSize + 1;
// 檢查查看的頁面數(shù)是否在范圍內(nèi)
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 貨物號(hào)
* @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 :得到要顯示的頁數(shù)
* @return int
*/
public int getPage() {
return page;
}
/**
* void setPage(int newpage)
* Description :修改要顯示的頁數(shù)
* @param int
*/
public void setPage(int newpage) {
page = newpage;
}
/**
* int getPageSize()
* Description :得到每頁要顯示的商品數(shù)
* @return int
*/
public int getPageSize() { // 每頁顯示的商品數(shù)
return pageSize;
}
/**
* void setPageSize(int newpsize)
* Description :修改每頁要顯示的商品數(shù)
* @param int
*/
public void setPageSize(int newpsize) {
pageSize = newpsize;
}
/**
* int getPageCount()
* Description :得到頁面總數(shù)
* @return int
*/
public int getPageCount() { // 頁面總數(shù)
return pageCount;
}
/**
* void setPageCount(int newpcount)
* Description :修改頁面總數(shù)
* @param int
*/
public void setPageCount(int newpcount) {
pageCount = newpcount;
}
/**
* int getRecordCount()
* Description :得到記錄總數(shù)
* @return long
*/
public long getRecordCount() {
return recordCount;
}
/**
* void setRecordCount(long newrcount)
* Description :修改記錄總數(shù)
* @param long
*/
public void setRecordCount(long newrcount) {
recordCount = newrcount;
}
/**
* Vector getProducts()
* Description :得到存儲(chǔ)商品的Vector
* @return Vector
*/
public Vector getProducts() {
return products;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -