📄 fooddao.java
字号:
package com.yy.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yy.bean.FoodBean;
public class FoodDao extends DButils{
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql;
public boolean addFood(FoodBean food){
boolean bool=false;
try {
con=this.getDataSource().getConnection();
sql="insert into foodInfo values (?,?,?,?,?)";
ps=con.prepareStatement(sql);
ps.setString(1,food.getFoodname());
ps.setString(2, food.getFoodImg());
ps.setDouble(3,food.getFoodprice());
ps.setString(4,food.getDescription());
ps.setString(5,food.getRemark());
int num=ps.executeUpdate();
if(num>0){
bool=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closePs();
this.closeCon();
}
return bool;
}
public List getFood(){
List list=new ArrayList();
try {
con=this.getDataSource().getConnection();
sql="select * from foodInfo";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
FoodBean food=new FoodBean();
food.setId(rs.getInt("id"));
food.setFoodname(rs.getString("foodname"));
food.setFoodImg(rs.getString("foodImg"));
food.setFoodprice(rs.getDouble("foodprice"));
food.setDescription(rs.getString("description"));
food.setRemark(rs.getString("remark"));
list.add(food);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeRs();
this.closePs();
this.closeCon();
}
return list;
}
public FoodBean GetfoodId(int id){
FoodBean food=null;
//List list=new ArrayList();
try {
con=this.getDataSource().getConnection();
sql="select * from foodInfo where id=?";
ps=con.prepareStatement(sql);
ps.setObject(1 , new Integer(id));
rs=ps.executeQuery();
while(rs.next()){
food=new FoodBean();
food.setId(rs.getInt("id"));
food.setFoodname(rs.getString("foodname"));
food.setFoodImg(rs.getString("foodImg"));
food.setFoodprice(rs.getDouble("foodprice"));
food.setDescription(rs.getString("description"));
food.setRemark(rs.getString("remark"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeRs();
this.closePs();
this.closeCon();
}
return food;
}
public Map addCart(Object id){
Map map=new HashMap();
try {
con=this.getDataSource().getConnection();
sql="select * from foodInfo where id=?";
ps=con.prepareStatement(sql);
ps.setObject(1,id);
rs=ps.executeQuery();
while(rs.next()){
FoodBean food=new FoodBean();
food.setId(rs.getInt("id"));
food.setFoodname(rs.getString("foodname"));
food.setFoodImg(rs.getString("foodImg"));
food.setFoodprice(rs.getDouble("foodprice"));
food.setDescription(rs.getString("description"));
food.setRemark(rs.getString("remark"));
food.setCount(rs.getInt("count"));
map.put(id, food);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeRs();
this.closePs();
this.closeCon();
}
return map;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -