package mybean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
//import java.sql.Statement;
//import java.util.ArrayList;
public class FoodDAO {
private PreparedStatement pst;
private java.sql.Statement st;
private ResultSet rs;
private Connection con;
private FoodVO food; // 实体bean
public FoodVO getFood() {
return food;
}
public void setFood(FoodVO food) {
this.food = food;
}
public FoodDAO() { // 利用构造函数连接数据库
con = DBUtil.connectDB();
}
public ArrayList<FoodVO> getAllfoods(){
ArrayList<FoodVO> allFoods=new ArrayList<FoodVO>();
try{
st= con.createStatement();
rs= st.executeQuery("select * from user");
while(rs.next()){
String thingID=rs.getString(1);
String shopname=rs.getString(2);
int pay=rs.getInt(3);
String foodname=rs.getString(4);
FoodVO food=new FoodVO();
food.setThingID(thingID);
food.setShopname(shopname);
food.setPay(pay);
food.setFoodname(foodname);
allFoods.add(food);
}
con.close();
}catch(SQLException e){
System.out.println("查看失败");
}
return allFoods;
}
public boolean insert() {
try {
pst= con.prepareStatement("insert into user values(?,?,?,?)");
pst.setString(1, food.getThingID());
pst.setString(2, food.getShopname());
pst.setInt(3, food.getPay());
pst.setString(4, food.getFoodname());
if(pst.executeUpdate()==1){
con.close();
return true;
}else{
con.close();
return false;
}
} catch (SQLException e) {
//System.out.print("增加失败");
System.out.print(e);
return false;
}
}
public ArrayList<FoodVO> getAllFoods(String fn){
ArrayList<FoodVO> allFoods=new ArrayList<FoodVO>();
try{
st=con.createStatement();
rs=st.executeQuery("select * from user where 商品名='"+fn+"'");
while(rs.next()){
String thingID=rs.getString("thingID");
String shopname=rs.getString("shopname");
int pay=rs.getInt("pay");
String foodname=rs.getString("foodname");
FoodVO food=new FoodVO();
food.setThingID(thingID);
food.setShopname(shopname);
food.setPay(pay);
food.setFoodname(foodname);
allFoods.add(food);
}
con.close();
}catch(SQLException e){
System.out.println("查询失败");
System.out.println(e);
}
return allFoods;
}
public ArrayList<FoodVO> deleteAllFoods(String p){
ArrayList<FoodVO> allFoods=new ArrayList<FoodVO>();
try{
st=con.createStatement();
st.executeUpdate("delete * from user where 商品名='"+p+"'");
rs=st.executeQuery("select * from user");
while(rs.next()){
String thingID=rs.getString("thingID");
String shopname=rs.getString("shopname");
int pay=rs.getInt("pay");
String foodname=rs.getString("foodname");
FoodVO food=new FoodVO();
food.setThingID(thingID);
food.setShopname(shopname);
food.setPay(pay);
food.setFoodname(foodname);
allFoods.add(food);
}
con.close();
}catch(SQLException e){
System.out.println("删除失败");
System.out.println(e);
}
return allFoods;
}
}