package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import util.closeConnection;
import util.createConnection;
import util.notFindUserException;
import bean.disc;
import bean.orderFoodInfo;
import bean.orderUserInfo;
import bean.userInfo;
import dao.IdiscDao;
public class CdiscDaoImpl implements IdiscDao {
public boolean checkUserPass(userInfo ui)throws notFindUserException{
Connection con=createConnection.getCon1();
PreparedStatement ps=null;
ResultSet rs=null;
boolean flag=false;
String sql="select * from userInfo where uname=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, ui.getUname());
rs=ps.executeQuery();
while(rs.next()){
if(rs.getString("upass").equals(ui.getUpass())){
flag=true;
}else{
flag=false;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection.closeAll(rs,ps,con);
}
return flag;
}
public List<disc> queryAllDisc() {
Connection con=createConnection.getCon();
PreparedStatement ps=null;
ResultSet rs=null;
List<disc>list=new ArrayList();
disc di=null;
String sql="select * from DiscInfo";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
di=new disc();
di.setId(rs.getInt("id"));
di.setItemID(rs.getString("itemID"));
di.setShortDescription(rs.getString("shortDescription"));
di.setLongDescription(rs.getString("longDescription"));
di.setImage(rs.getString("image"));
di.setCost(rs.getFloat("cost"));
list.add(di);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection.closeAll(rs,ps,con);
}
return list;
}
public disc queryDiscById(String itemID) {
Connection con=createConnection.getCon();
PreparedStatement ps=null;
ResultSet rs=null;
disc di=null;
String sql="select * from DiscInfo where itemID=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, itemID);
rs=ps.executeQuery();
while(rs.next()){
di=new disc();
di.setId(rs.getInt("id"));
di.setItemID(rs.getString("itemID"));
di.setShortDescription(rs.getString("shortDescription"));
di.setLongDescription(rs.getString("longDescription"));
di.setImage(rs.getString("image"));
di.setCost(rs.getFloat("cost"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection.closeAll(rs,ps,con);
}
return di;
}
public boolean saveOrderFoodInfoByList(List list, String name) {
System.out.println("$$$$$$$$$$$$"+list.size());
Connection con=createConnection.getCon();
PreparedStatement ps=null;
int num=0;
boolean flag=false;
String sql="insert into orderFoodInfo(itemID,shortDescription,cost,num,sum)"
+"values(?,?,?,?,?)";
try {
ps=con.prepareStatement(sql);
for(int i=0;i<list.size();i++){
orderFoodInfo ofi=(orderFoodInfo)list.get(i);
ps.setString(1, ofi.getItemID());
ps.setString(2, ofi.getShortDescription());
ps.setFloat(3, ofi.getCost());
ps.setInt(4, ofi.getNum());
ps.setFloat(5, ofi.getSum());
num=ps.executeUpdate();
}
if(num==0){
flag=false;
}else{
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection.closeTow(ps, con);
}
return flag;
}
public boolean saveOrderFoodInfoByMap(Map map, String name) {
Connection con=createConnection.getCon();
PreparedStatement ps=null;
int num=0;
boolean flag=false;
String sql="insert into orderFoodInfo(itemID,shortDescription,cost,num,sum)"
+"values(?,?,?,?,?)";
Set <String>set=map.keySet();
try {
ps=con.prepareStatement(sql);
for(String str:set){
orderFoodInfo ofi=(orderFoodInfo)map.get(str);
ps.setString(1, ofi.getItemID());
ps.setString(2, ofi.getShortDescription());
ps.setFloat(3, ofi.getCost());
ps.setInt(4, ofi.getNum());
ps.setFloat(5, ofi.getSum());
num=ps.executeUpdate();
}
if(num==0){
flag=false;
}else{
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection.closeTow(ps, con);
}
return flag;
}
public boolean saveOrderUserInfo(orderUserInfo oui) {
Connection con=createConnection.getCon();
PreparedStatement ps=null;
boolean flag=false;
int num=0;;
String sql="insert into orderUserInfo(addr,userName,tel) values(?,?,?)";
try {
ps=con.prepareStatement(sql);
ps.setString(1, oui.getAddr());
ps.setString(2, oui.getUserName());
ps.setString(3, oui.getTel());
num=ps.executeUpdate();
if(num==0){
flag=false;
}else{
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection.closeTow(ps, con);
}
return flag;
}
public boolean saveuserInfo(userInfo ui) {
Connection con=createConnection.getCon();
PreparedStatement ps=null;
boolean flag=false;
int num=0;;
String sql="insert into userInfo(uname,upass,sex,age)"
+" values(?,?,?,?)";
try {
ps=con.prepareStatement(sql);
ps.setString(1, ui.getUname());
ps.setString(2, ui.getUpass());
ps.setString(3, ui.getSex());
ps.setInt(4, ui.getAge());
num=ps.executeUpdate();
if(num==0){
flag=false;
}else{
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeConnection.closeTow(ps, con);
}
return flag;
}
}