购物车该怎么写 在Web开发中购物车该怎么写 最好是 包装好的代码 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没代码 给个思路 利用session原理吧你懂的 用session 的话 只能在一个进程中保存啊 我下次打开的时候 就看不到了呀 难到我在打开的时候 就从数据库读取啊 好麻烦额 我希望下次打开的时候 还是能看到 不用数据库 就用application对象处理 application他相当于是全局 变量啊 是任何人 打开网页都可以看到的啊 这样不好 用session或者 cookie 保存 cookie 咋用啊 不会也 http://download.csdn.net/source/328708 参考这个 DoCarDispatchAction.javapackage edu.gdut.rdc.foodorder.action;import java.util.ArrayList;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction;import edu.gdut.rdc.foodorder.dao.FoodDao;import edu.gdut.rdc.foodorder.entity.Food;import edu.gdut.rdc.foodorder.entity.ShopCarTo;import edu.gdut.rdc.foodorder.entity.User;public class DoCarDispatchAction extends DispatchAction { public ActionForward Buy(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user1"); String prompt = null; if(user == null) prompt = "user_unlogin"; else{ int jump = Integer.parseInt(request.getParameter("jump")); int foodid = Integer.parseInt(request.getParameter("foodid")); int classid = Integer.parseInt(request.getParameter("classid")); ArrayList buylist = (ArrayList) session.getAttribute("buylist"); if(buylist == null) buylist = new ArrayList(); FoodDao fdao = new FoodDao(); Food fooddetail = fdao.showDetailFood(foodid,classid); String name = fooddetail.getFood_name(); float price = fooddetail.getFood_price(); if(buylist.size()==0){ ShopCarTo sct = new ShopCarTo(); sct.setId(foodid); sct.setName(name); sct.setPrice(price); sct.setNum(1); buylist.add(sct); }else{ int i=0; for(;i < buylist.size();i++){ ShopCarTo sct = (ShopCarTo)buylist.get(i); if(sct.getId() == foodid){ sct.setNum(sct.getNum()+1); break; } } if(i >= buylist.size()){ ShopCarTo sct = new ShopCarTo(); sct.setId(foodid); sct.setName(name); sct.setPrice(price); sct.setNum(1); buylist.add(sct); } } session.setAttribute("buylist",buylist); //return mapping.getInputForward(); if(jump == 1) prompt = "buy_success1"; else if(jump == 2) prompt = "buy_success2"; else if(jump == 3) prompt = "buy_success3"; else prompt = null; } return mapping.findForward(prompt); /*if(jump == 1) return mapping.findForward("buy_success1"); else return mapping.findForward("buy_success2");*/ } public ActionForward Remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); int foodid = Integer.parseInt(request.getParameter("foodid")); ArrayList buylist = (ArrayList) session.getAttribute("buylist"); if(buylist == null) buylist = new ArrayList(); int i; for(i = 0;i < buylist.size();i++){ ShopCarTo sct = (ShopCarTo)buylist.get(i); if(sct.getId() == foodid){ if(sct.getNum() > 1){ sct.setNum(sct.getNum()-1); break; } else{ buylist.remove(i); } } } return mapping.findForward("del_success"); } public ActionForward Clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); ArrayList buylist = (ArrayList)session.getAttribute("buylist"); buylist.clear(); return mapping.findForward("del_success"); }} FoodDao.javapackage edu.gdut.rdc.foodorder.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import edu.gdut.rdc.foodorder.impl.IFood;import edu.gdut.rdc.foodorder.entity.Food;import edu.gdut.rdc.foodorder.tool.ConnDB;public class FoodDao implements IFood { public Connection conn = null; // 显示单个食物详细信息,同时当进行修改食物信息的时候也调用这个方法,用于传值 public Food showDetailFood(Integer food_id, Integer food_classid) { conn = ConnDB.getConn(); PreparedStatement ps = null; ResultSet RS = null; Food food = new Food(); String sql = null; String food_classname = null; try { FoodclassDao fcd = new FoodclassDao(); food_classname = fcd.returnFoodclassnameByFoodclassId(food_classid); /* * stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, * ResultSet.CONCUR_READ_ONLY); sql = "select *from t_food where * food_id=" + food_id; RS = stmt.executeQuery(sql); */ sql = "select *from t_food where food_id=?"; ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ps.setInt(1, food_id); RS = ps.executeQuery(); if (RS.next()) { food.setFood_id(food_id); food.setFood_name(RS.getString(2)); food.setFood_price(RS.getInt(4)); food.setFood_imgpath(RS.getString(3)); food.setFood_description(RS.getString(5)); food.setFood_numleft(RS.getInt(6)); food.setFood_classname(food_classname); food.setFood_classid(food_classid); food.setFood_goodnum(RS.getInt(7)); food.setFood_ordinarynum(RS.getInt(8)); food.setFood_ordernum(RS.getInt(9)); } System.out.println("生成详细food OK"); } catch (SQLException e) { e.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (RS != null) { try { RS.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } return food; } //前台评价商品 public boolean opinionfood(Integer food_id,Integer opinion) { conn = ConnDB.getConn(); PreparedStatement ps = null,ps2 = null; ResultSet RS= null; String sql = null; int judge=-1; try { sql="select *from t_food where food_id=?"; ps=conn.prepareStatement(sql); ps.setInt(1, food_id); RS = ps.executeQuery(); int food_goodnum; int food_ordinarynum; if(RS.next()){ if(opinion==1){ food_goodnum=RS.getInt(7)+1; sql="update t_food set food_goodnum=? where food_id=?"; ps2=conn.prepareStatement(sql); ps2.setInt(1, food_goodnum); ps2.setInt(2, food_id); judge=ps2.executeUpdate(); }else{ food_ordinarynum=RS.getInt(8)+1; sql="update t_food set food_ordinarynum=? where food_id=?"; ps2=conn.prepareStatement(sql); ps2.setInt(1, food_ordinarynum); ps2.setInt(2, food_id); judge=ps2.executeUpdate(); } } } catch (SQLException e) { e.printStackTrace(); } finally { if(ps2!=null){ try { ps2.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (RS != null) { try { RS.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } if (judge > 0) { return true; } else { return false; } } /*// 删除单个食物 public boolean deleteFood(Integer food_id) { conn = ConnDB.getConn(); PreparedStatement ps = null; String sql = null; int judge = -1; try { * stmt = conn.createStatement(); sql = "delete from t_food where * food_id=" + food_id; stmt.executeUpdate(sql); sql = "delete from t_food where food_id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, food_id); judge = ps.executeUpdate(); System.out.println("delete food right!"); } catch (SQLException e) { e.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } if (judge > 0) { return true; } else { return false; } }*/ //批量删除商品 public boolean deleteFood(String[] foodIdList) { conn = ConnDB.getConn(); Statement stmt = null; String sql = null; int judge = -1; StringBuffer sbfSql = new StringBuffer(); for (int i = 0; i < foodIdList.length; i++) { sbfSql.append("'").append(foodIdList[i]).append("'").append(","); } sql = "delete from t_food where food_id in (" + sbfSql.substring(0, sbfSql.length() - 1) + ")"; try { stmt = conn.createStatement(); stmt.executeUpdate(sql); judge = 1; } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } if (judge > 0) { return true; } else { return false; } } // 修改单个食物,在Action中由form生成food对象,应该是无法得到food_classid的吧 public boolean modifyFood(Food food) { conn = ConnDB.getConn(); PreparedStatement ps = null; String sql = null; int judge = -1; try { FoodclassDao fcd = new FoodclassDao(); Integer food_classid = fcd.returnFoodclassidByFoodclassName(food .getFood_classname()); String food_imgpath=food.getFood_imgpath(); if(food_imgpath.equals("empty")){ sql = "update t_food set food_name=?, food_price=?, food_classid=?, food_numleft=?, food_description=? where food_id=?"; ps = conn.prepareStatement(sql); ps.setString(1, food.getFood_name()); ps.setFloat(2, food.getFood_price()); ps.setInt(3, food_classid); ps.setInt(4, food.getFood_numleft()); ps.setString(5, food.getFood_description()); ps.setInt(6, food.getFood_id()); }else{ sql = "update t_food set food_name=?, food_price=?, food_classid=?, food_numleft=?, food_description=?, food_imgpath=? where food_id=?"; ps = conn.prepareStatement(sql); ps.setString(1, food.getFood_name()); ps.setFloat(2, food.getFood_price()); ps.setInt(3, food_classid); ps.setInt(4, food.getFood_numleft()); ps.setString(5, food.getFood_description()); ps.setString(6, food_imgpath); ps.setInt(7, food.getFood_id()); } judge = ps.executeUpdate(); System.out.println("update food right!"); } catch (SQLException e) { e.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } if (judge > 0) { return true; } else { return false; } } // 添加食物 public boolean addFood(Food food) { conn = ConnDB.getConn(); PreparedStatement ps = null; String sql = null; int judge = -1; Integer class_id = null; try { FoodclassDao fcd = new FoodclassDao(); class_id = fcd.returnFoodclassidByFoodclassName(food .getFood_classname()); sql = "insert into t_food(food_name,food_price,food_numleft,food_classid,food_imgpath,food_description) values(?,?,?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, food.getFood_name()); ps.setFloat(2, food.getFood_price()); ps.setInt(3, food.getFood_numleft()); ps.setInt(4, class_id); ps.setString(5, food.getFood_imgpath()); ps.setString(6, food.getFood_description()); judge = ps.executeUpdate(); System.out.println("insert food right!"); } catch (SQLException e) { e.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } if (judge > 0) { return true; } else { return false; } } //推荐取消推荐商品 public boolean recfood(int food_recommend,int food_id){ conn=ConnDB.getConn(); PreparedStatement ps = null; String sql = null; int judge = -1; try{ sql="update t_food set food_recommend=? where food_id=?"; ps=conn.prepareStatement(sql); ps.setInt(1, food_recommend); ps.setInt(2, food_id); judge=ps.executeUpdate(); }catch (SQLException e) { e.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } if (judge > 0) { return true; } else { return false; }} } 接上面的FoodDao.java // 查询食物 public List<Food> selectFood(String selectname,int recordBegin,int recordLimit) { conn = ConnDB.getConn(); List<Food> selectlist = new ArrayList(); ResultSet RS = null; Statement stmt = null; String sql = null; try { stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); sql = "select *from t_food where food_name like '%" + selectname + "%' order by food_id desc limit "+recordBegin+","+recordLimit; RS = stmt.executeQuery(sql); while (RS.next()) { Food food = new Food(); food.setFood_id(RS.getInt(1)); food.setFood_name(RS.getString(2)); food.setFood_imgpath(RS.getString(3)); food.setFood_price(RS.getFloat(4)); food.setFood_description(RS.getString(5)); food.setFood_numleft(RS.getInt(6)); food.setFood_classid(RS.getInt(10)); FoodclassDao fcd = new FoodclassDao(); String food_classname = fcd.returnFoodclassnameByFoodclassId(RS .getInt(10)); food.setFood_classname(food_classname); selectlist.add(food); } System.out.println("food selectlist 生成!"); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (RS != null) { try { RS.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } return selectlist; }// 前台首页显示新品 public List<Food> showNewFood() { conn = ConnDB.getConn(); List<Food> newfoodlist = new ArrayList(); ResultSet RS = null; Statement stmt = null; String sql = null; try { sql = "select *from t_food order by food_id desc limit 0,4"; stmt = conn.createStatement(); RS = stmt.executeQuery(sql); while (RS.next()) { Food food = new Food(); food.setFood_name(RS.getString(2)); food.setFood_price(RS.getInt(4)); food.setFood_description(RS.getString(5)); food.setFood_imgpath(RS.getString(3)); food.setFood_classid(RS.getInt(10)); food.setFood_id(RS.getInt(1)); newfoodlist.add(food); } System.out.println("newfoodlist 生成!"); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (RS != null) { try { RS.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } return newfoodlist; } //前台显示推荐商品 public List<Food> showRecommendFood() { conn = ConnDB.getConn(); List<Food> recommendfoodlist = new ArrayList(); ResultSet RS = null; Statement stmt = null; String sql = null; try { sql = "select *from t_food where food_recommend=1"; stmt = conn.createStatement(); RS = stmt.executeQuery(sql); while (RS.next()) { Food food = new Food(); food.setFood_name(RS.getString(2)); food.setFood_price(RS.getInt(4)); food.setFood_description(RS.getString(5)); food.setFood_imgpath(RS.getString(3)); food.setFood_classid(RS.getInt(10)); food.setFood_id(RS.getInt(1)); recommendfoodlist.add(food); } System.out.println("recommendfoodlist 生成!"); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (RS != null) { try { RS.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } return recommendfoodlist; } //前台显示销售排行 public List<Food> salesRank() { conn = ConnDB.getConn(); List<Food> salesrank = new ArrayList(); ResultSet RS = null; Statement stmt = null; String sql = null; try{ sql = "select *from t_food order by food_ordernum desc limit 0,7"; stmt = conn.createStatement(); RS = stmt.executeQuery(sql); while (RS.next()) { Food food=new Food(); food.setFood_name(RS.getString(2)); food.setFood_ordernum(RS.getInt(9)); food.setFood_classid(RS.getInt(10)); food.setFood_id(RS.getInt(1)); salesrank.add(food); } System.out.println("salesrank 生成!"); }catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (RS != null) { try { RS.close(); } catch (SQLException e) { e.printStackTrace(); } } ConnDB.close(); } return salesrank; } 正常的时候还是用session,并存入数据库,当关闭浏览器的时候session就没了,就无法去了。application是公用的,不能都存到哪里啊,归根结底还得用数据库,要不就文件 一个表userid,物品id,每次打开读一下不就行了吗?怎么问的这么复杂!有必要吗? submit提交后 显示一个空白页面 没有显示当前页面 action没有设置 为什么啊 求解答 filter 一个数据库连接问题,请求各位帮忙! 新手提问!! 再问一个context问题 如何用jsp得到文件的修改时间? 请教,我的TimerTask怎么不起作用了? jsp+tomcat+sql server 2000调用数据库出错. 我现在用JSP有一段时间了,写JSP,BEAN,都可以了,也可以写简单的SERVLET了,有个问题问问大家。 谁能给我告诉 session 和 request 的那个存储区的范围大? SSH 整合问题 速求各位帮帮忙! 如果实现QQ空间心情回复效果。
用session 的话 只能在一个进程中保存啊
我下次打开的时候 就看不到了呀 难到我在打开的时候 就从数据库读取啊 好麻烦额 我希望下次打开的时候 还是能看到
不用数据库 就用application对象处理
cookie 咋用啊 不会也
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import edu.gdut.rdc.foodorder.dao.FoodDao;
import edu.gdut.rdc.foodorder.entity.Food;
import edu.gdut.rdc.foodorder.entity.ShopCarTo;
import edu.gdut.rdc.foodorder.entity.User;
public class DoCarDispatchAction extends DispatchAction { public ActionForward Buy(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user1");
String prompt = null;
if(user == null)
prompt = "user_unlogin";
else{
int jump = Integer.parseInt(request.getParameter("jump"));
int foodid = Integer.parseInt(request.getParameter("foodid"));
int classid = Integer.parseInt(request.getParameter("classid"));
ArrayList buylist = (ArrayList) session.getAttribute("buylist");
if(buylist == null)
buylist = new ArrayList();
FoodDao fdao = new FoodDao();
Food fooddetail = fdao.showDetailFood(foodid,classid);
String name = fooddetail.getFood_name();
float price = fooddetail.getFood_price();
if(buylist.size()==0){
ShopCarTo sct = new ShopCarTo();
sct.setId(foodid);
sct.setName(name);
sct.setPrice(price);
sct.setNum(1);
buylist.add(sct);
}else{
int i=0;
for(;i < buylist.size();i++){
ShopCarTo sct = (ShopCarTo)buylist.get(i);
if(sct.getId() == foodid){
sct.setNum(sct.getNum()+1);
break;
}
}
if(i >= buylist.size()){
ShopCarTo sct = new ShopCarTo();
sct.setId(foodid);
sct.setName(name);
sct.setPrice(price);
sct.setNum(1);
buylist.add(sct);
}
}
session.setAttribute("buylist",buylist);
//return mapping.getInputForward();
if(jump == 1)
prompt = "buy_success1";
else if(jump == 2)
prompt = "buy_success2";
else if(jump == 3)
prompt = "buy_success3";
else
prompt = null;
}
return mapping.findForward(prompt);
/*if(jump == 1)
return mapping.findForward("buy_success1");
else
return mapping.findForward("buy_success2");*/
}
public ActionForward Remove(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
int foodid = Integer.parseInt(request.getParameter("foodid"));
ArrayList buylist = (ArrayList) session.getAttribute("buylist");
if(buylist == null)
buylist = new ArrayList();
int i;
for(i = 0;i < buylist.size();i++){
ShopCarTo sct = (ShopCarTo)buylist.get(i);
if(sct.getId() == foodid){
if(sct.getNum() > 1){
sct.setNum(sct.getNum()-1);
break;
}
else{
buylist.remove(i);
}
}
}
return mapping.findForward("del_success");
}
public ActionForward Clear(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
ArrayList buylist = (ArrayList)session.getAttribute("buylist");
buylist.clear();
return mapping.findForward("del_success");
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import edu.gdut.rdc.foodorder.impl.IFood;
import edu.gdut.rdc.foodorder.entity.Food;
import edu.gdut.rdc.foodorder.tool.ConnDB;public class FoodDao implements IFood { public Connection conn = null; // 显示单个食物详细信息,同时当进行修改食物信息的时候也调用这个方法,用于传值
public Food showDetailFood(Integer food_id, Integer food_classid) {
conn = ConnDB.getConn(); PreparedStatement ps = null;
ResultSet RS = null;
Food food = new Food();
String sql = null;
String food_classname = null;
try {
FoodclassDao fcd = new FoodclassDao();
food_classname = fcd.returnFoodclassnameByFoodclassId(food_classid);
/*
* stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
* ResultSet.CONCUR_READ_ONLY); sql = "select *from t_food where
* food_id=" + food_id; RS = stmt.executeQuery(sql);
*/
sql = "select *from t_food where food_id=?";
ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ps.setInt(1, food_id);
RS = ps.executeQuery();
if (RS.next()) {
food.setFood_id(food_id);
food.setFood_name(RS.getString(2));
food.setFood_price(RS.getInt(4));
food.setFood_imgpath(RS.getString(3));
food.setFood_description(RS.getString(5));
food.setFood_numleft(RS.getInt(6));
food.setFood_classname(food_classname);
food.setFood_classid(food_classid);
food.setFood_goodnum(RS.getInt(7));
food.setFood_ordinarynum(RS.getInt(8));
food.setFood_ordernum(RS.getInt(9));
}
System.out.println("生成详细food OK");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (RS != null) {
try {
RS.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
return food;
} //前台评价商品
public boolean opinionfood(Integer food_id,Integer opinion) {
conn = ConnDB.getConn();
PreparedStatement ps = null,ps2 = null;
ResultSet RS= null;
String sql = null;
int judge=-1;
try {
sql="select *from t_food where food_id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, food_id);
RS = ps.executeQuery();
int food_goodnum;
int food_ordinarynum;
if(RS.next()){
if(opinion==1){
food_goodnum=RS.getInt(7)+1;
sql="update t_food set food_goodnum=? where food_id=?";
ps2=conn.prepareStatement(sql);
ps2.setInt(1, food_goodnum);
ps2.setInt(2, food_id);
judge=ps2.executeUpdate();
}else{
food_ordinarynum=RS.getInt(8)+1;
sql="update t_food set food_ordinarynum=? where food_id=?";
ps2=conn.prepareStatement(sql);
ps2.setInt(1, food_ordinarynum);
ps2.setInt(2, food_id);
judge=ps2.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(ps2!=null){
try {
ps2.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (RS != null) {
try {
RS.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
if (judge > 0) {
return true;
} else {
return false;
}
}
/*// 删除单个食物
public boolean deleteFood(Integer food_id) {
conn = ConnDB.getConn(); PreparedStatement ps = null; String sql = null;
int judge = -1;
try {
* stmt = conn.createStatement(); sql = "delete from t_food where
* food_id=" + food_id; stmt.executeUpdate(sql);
sql = "delete from t_food where food_id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, food_id);
judge = ps.executeUpdate();
System.out.println("delete food right!");
} catch (SQLException e) { e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
if (judge > 0) {
return true;
} else {
return false;
}
}*/ //批量删除商品
public boolean deleteFood(String[] foodIdList) {
conn = ConnDB.getConn();
Statement stmt = null;
String sql = null;
int judge = -1;
StringBuffer sbfSql = new StringBuffer();
for (int i = 0; i < foodIdList.length; i++) {
sbfSql.append("'").append(foodIdList[i]).append("'").append(",");
}
sql = "delete from t_food where food_id in ("
+ sbfSql.substring(0, sbfSql.length() - 1) + ")";
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
judge = 1;
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
if (judge > 0) {
return true;
} else {
return false;
}
}
// 修改单个食物,在Action中由form生成food对象,应该是无法得到food_classid的吧
public boolean modifyFood(Food food) {
conn = ConnDB.getConn(); PreparedStatement ps = null; String sql = null;
int judge = -1;
try {
FoodclassDao fcd = new FoodclassDao();
Integer food_classid = fcd.returnFoodclassidByFoodclassName(food
.getFood_classname());
String food_imgpath=food.getFood_imgpath();
if(food_imgpath.equals("empty")){
sql = "update t_food set food_name=?, food_price=?, food_classid=?, food_numleft=?, food_description=? where food_id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, food.getFood_name());
ps.setFloat(2, food.getFood_price());
ps.setInt(3, food_classid);
ps.setInt(4, food.getFood_numleft());
ps.setString(5, food.getFood_description());
ps.setInt(6, food.getFood_id());
}else{
sql = "update t_food set food_name=?, food_price=?, food_classid=?, food_numleft=?, food_description=?, food_imgpath=? where food_id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, food.getFood_name());
ps.setFloat(2, food.getFood_price());
ps.setInt(3, food_classid);
ps.setInt(4, food.getFood_numleft());
ps.setString(5, food.getFood_description());
ps.setString(6, food_imgpath);
ps.setInt(7, food.getFood_id());
}
judge = ps.executeUpdate();
System.out.println("update food right!"); } catch (SQLException e) { e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close(); } catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
if (judge > 0) {
return true;
} else {
return false;
} } // 添加食物
public boolean addFood(Food food) {
conn = ConnDB.getConn();
PreparedStatement ps = null;
String sql = null;
int judge = -1;
Integer class_id = null;
try {
FoodclassDao fcd = new FoodclassDao();
class_id = fcd.returnFoodclassidByFoodclassName(food
.getFood_classname());
sql = "insert into t_food(food_name,food_price,food_numleft,food_classid,food_imgpath,food_description) values(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, food.getFood_name());
ps.setFloat(2, food.getFood_price());
ps.setInt(3, food.getFood_numleft());
ps.setInt(4, class_id);
ps.setString(5, food.getFood_imgpath());
ps.setString(6, food.getFood_description());
judge = ps.executeUpdate();
System.out.println("insert food right!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
if (judge > 0) {
return true;
} else {
return false;
}
}
//推荐取消推荐商品
public boolean recfood(int food_recommend,int food_id){
conn=ConnDB.getConn();
PreparedStatement ps = null;
String sql = null;
int judge = -1;
try{
sql="update t_food set food_recommend=? where food_id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, food_recommend);
ps.setInt(2, food_id);
judge=ps.executeUpdate();
}catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
if (judge > 0) {
return true;
} else {
return false;
}
}
}
public List<Food> selectFood(String selectname,int recordBegin,int recordLimit) {
conn = ConnDB.getConn(); List<Food> selectlist = new ArrayList();
ResultSet RS = null;
Statement stmt = null;
String sql = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY); sql = "select *from t_food where food_name like '%" + selectname
+ "%' order by food_id desc limit "+recordBegin+","+recordLimit; RS = stmt.executeQuery(sql);
while (RS.next()) {
Food food = new Food();
food.setFood_id(RS.getInt(1));
food.setFood_name(RS.getString(2));
food.setFood_imgpath(RS.getString(3));
food.setFood_price(RS.getFloat(4));
food.setFood_description(RS.getString(5));
food.setFood_numleft(RS.getInt(6));
food.setFood_classid(RS.getInt(10)); FoodclassDao fcd = new FoodclassDao();
String food_classname = fcd.returnFoodclassnameByFoodclassId(RS
.getInt(10)); food.setFood_classname(food_classname);
selectlist.add(food); }
System.out.println("food selectlist 生成!"); } catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (RS != null) {
try {
RS.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
return selectlist;
}
// 前台首页显示新品
public List<Food> showNewFood() {
conn = ConnDB.getConn(); List<Food> newfoodlist = new ArrayList();
ResultSet RS = null;
Statement stmt = null;
String sql = null; try {
sql = "select *from t_food order by food_id desc limit 0,4";
stmt = conn.createStatement();
RS = stmt.executeQuery(sql);
while (RS.next()) {
Food food = new Food();
food.setFood_name(RS.getString(2));
food.setFood_price(RS.getInt(4));
food.setFood_description(RS.getString(5));
food.setFood_imgpath(RS.getString(3));
food.setFood_classid(RS.getInt(10));
food.setFood_id(RS.getInt(1));
newfoodlist.add(food); }
System.out.println("newfoodlist 生成!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (RS != null) {
try {
RS.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
return newfoodlist;
}
//前台显示推荐商品
public List<Food> showRecommendFood() {
conn = ConnDB.getConn(); List<Food> recommendfoodlist = new ArrayList();
ResultSet RS = null;
Statement stmt = null;
String sql = null; try {
sql = "select *from t_food where food_recommend=1";
stmt = conn.createStatement();
RS = stmt.executeQuery(sql);
while (RS.next()) {
Food food = new Food();
food.setFood_name(RS.getString(2));
food.setFood_price(RS.getInt(4));
food.setFood_description(RS.getString(5));
food.setFood_imgpath(RS.getString(3));
food.setFood_classid(RS.getInt(10));
food.setFood_id(RS.getInt(1));
recommendfoodlist.add(food); }
System.out.println("recommendfoodlist 生成!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (RS != null) {
try {
RS.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
return recommendfoodlist;
}
//前台显示销售排行
public List<Food> salesRank() {
conn = ConnDB.getConn();
List<Food> salesrank = new ArrayList();
ResultSet RS = null;
Statement stmt = null;
String sql = null;
try{
sql = "select *from t_food order by food_ordernum desc limit 0,7";
stmt = conn.createStatement();
RS = stmt.executeQuery(sql);
while (RS.next()) {
Food food=new Food();
food.setFood_name(RS.getString(2));
food.setFood_ordernum(RS.getInt(9));
food.setFood_classid(RS.getInt(10));
food.setFood_id(RS.getInt(1));
salesrank.add(food);
}
System.out.println("salesrank 生成!");
}catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (RS != null) {
try {
RS.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
ConnDB.close();
}
return salesrank;
}