📄 d04a155d7c45001c1e82be6cd01e78c9
字号:
package hall;
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();
}
public boolean getRequest(javax.servlet.http.HttpServletRequest newrequest) {
boolean flag = false;
//producItem
try {
request = newrequest;
String item = request.getParameter("producItem");
int productItem = 0;
try {
productItem = Integer.parseInt(item);
} catch (Exception e) {
}
prod.setProductorItem(productItem);
//producName
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);
//iaAptitude
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;
}
}
/**
* 完成商品添加
*/
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 {
System.out.print(sqlStr);
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;
}
}
/**
* 完成图书删除
* @param aid
* @return
* @throws java.lang.Exception
*/
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;
}
}
////////////////////////////////////////////////////////
public boolean product_search(String inProdName,String inType,String saccount1,String saccount2) throws Exception {
//request = res;
String type = inType;
String prodName = inProdName;
/*String saccount1 = request.getParameter("account1");
String saccount2 = request.getParameter("account2");*/
double account1;
double account2;
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";
}
System.out.println(sqlStr1);
try {
ResultSet rs1 = myConnection.runQuery(sqlStr1);
if (rs1.next())
recordCount = rs1.getInt(1);
rs1.close();
System.out.println(recordCount);
} 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;
//sql为倒序取值
System.out.println(sqlStr2);
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;
}
}
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("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);
flag = true;
} else {
flag = false;
}
}catch(Exception e){
System.out.println(e);
flag = false;
}
return flag;
}
public int getPage() { //显示的页码
return page;
}
public void setPage(int newpage) {
page = newpage;
}
public int getPageSize() { //每页显示的图书数
return pageSize;
}
public void setPageSize(int newpsize) {
pageSize = newpsize;
}
public int getPageCount() { //页面总数
return pageCount;
}
public void setPageCount(int newpcount) {
pageCount = newpcount;
}
public long getRecordCount() {
return recordCount;
}
public void setRecordCount(long newrcount) {
recordCount = newrcount;
}
public Vector getProducts() {
return products;
}
/////////////////////////////////
//public static void main(String[] args){
//try{
// opProduct op1 = new opProduct();
// op1.product_search("a", "b", 2, 1234);
// }
// catch(Exception e){
//System.out.println(e);
//}
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -