package hao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class DB {
public boolean check(String userName, String password){
boolean flag=false;
Connection con=null;
PreparedStatement st=null;
ResultSet rs=null;
try {
con=ConnectionPool.conPool.getCon();
st=con.prepareStatement("select users.userName,users.password from users where userName=? and password=?");
st.setString(1,userName);
st.setString(2,password);
rs=st.executeQuery();
if(rs.next()){
System.out.println("成功!");
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
rs.close();
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
public List food(Food fan){
Connection con=null;
List list=new ArrayList();
PreparedStatement st=null;
ResultSet rs=null;
try {
con=ConnectionPool.conPool.getCon();
st=con.prepareStatement("select id,name,price,kouwei,shuliang,tupian from fd ");
rs=st.executeQuery();
while(rs.next()){
Food fan1=new Food();
fan1.setId(rs.getInt("id"));
fan1.setName(rs.getString("name"));
fan1.setPrice(rs.getBigDecimal("price"));
fan1.setKouwei(rs.getString("kouwei"));
fan1.setShuliang(rs.getInt("shuliang"));
fan1.setTupian(rs.getString("tupian"));
list.add(fan1);
for (int a=0;a<list.size();a++){
Food jj=(Food)list.get(a);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
rs.close();
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public int hao(Users user){
int count=0;
Connection con=null;
PreparedStatement st=null;
try {
con=ConnectionPool.conPool.getCon();
st=con.prepareStatement("insert into users(emailName,password,userName,sex,addressType,locus,street,address,deptName,handsetTel,tel,elseAddress) values(?,?,?,?,?,?,?,?,?,?,?,?)");
st.setString(1, user.getEmailName()) ;
st.setString(2, user.getPassword()) ;
st.setString(3, user.getUserName()) ;
st.setString(4, user.getSex()) ;
st.setString(5, user.getAddressType()) ;
st.setString(6, user.getLocus()) ;
st.setString(7, user.getStreet()) ;
st.setString(8, user.getAddress()) ;
st.setString(9, user.getDeptName()) ;
st.setString(10, user.getHandsetTel()) ;
st.setString(11, user.getTel()) ;
st.setString(12, user.getElseAddress()) ;
count = st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
public Foods getFoodInfo(Foods Fd){
Connection con=null;
PreparedStatement st=null;
ResultSet rs=null;
try {
con=ConnectionPool.conPool.getCon();
st=con.prepareStatement("select name,price from fd where id="+Fd.getId());
rs=st.executeQuery();
while(rs.next()){
Fd.setName(rs.getString("name"));
Fd.setPrice(rs.getBigDecimal("price"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
rs.close();
st.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Fd;
}
}
public String huai(biaodan bd){
ResultSet rs=null;
Connection con=null;
PreparedStatement st=null;
int count=0;
String tempid=null;
Calendar myDate = Calendar.getInstance(); //取得系统时间
int sYear = myDate.get(Calendar.YEAR);
int sMonth = myDate.get(Calendar.MONTH)+1;
int sDate = myDate.get(Calendar.DATE);
String sYear1 = sYear+"";
String sMonth1 =sMonth+"";
String sDate1 = sDate+"";
tempid =sYear1+sMonth1+sDate1;
String tempidd = tempid+"0001";
System.out.println(tempid);
try{
con = ConnectionPool.conPool.getCon();
String sql1 = "select max(id) as id from biaodian where id like '"+tempid+"%'";
st = con.prepareStatement(sql1);
rs = st.executeQuery();
if(rs.next()){
String st1 = rs.getString("id");
if(st1!=null){
long templong = Long.parseLong(st1)+1;
tempid = templong+"";
bd.setId(tempid);
}else{
bd.setId(tempidd);
tempid = tempidd;
}
}
con=ConnectionPool.conPool.getCon();
st=con.prepareStatement("insert into biaodian(id,dizhi,lianxiren,dianhua,shijian,beizhu,total) values(?,?,?,?,?,?,?)");
st.setString(1,bd.getId());
System.out.println(bd.getId());
st.setString(2,bd.getDizhi());
st.setString(3,bd.getLianxiren());
st.setString(4,bd.getDianhua());
st.setString(5,bd.getShijian());
st.setString(6,bd.getBeizhu());
st.setBigDecimal(7, bd.getTotal());
count = st.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
rs.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return tempid;
}
public int shop(Foods fs,String a){
Connection con=null;
PreparedStatement st=null;
int count=0;
try {
con=ConnectionPool.conPool.getCon();
st=con.prepareStatement("insert into shop(shopId,bdId,num) values(?,?,?)");
st.setInt(1, fs.getId());
st.setString(2, a);
st.setInt(3, fs.getNum());
count = st.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
public void addgoods(Food fod){
int count=0;
Connection con=null;
PreparedStatement st=null;
try {
con=ConnectionPool.conPool.getCon();
st=con.prepareStatement("insert into fd(name,price,kouwei,tupian) values(?,?,?,?)");
st.setString(1,fod.getName());
st.setBigDecimal(2,fod.getPrice());
st.setString(3,fod.getKouwei());
st.setString(4, fod.getTupian());
count=st.executeUpdate();
System.out.println("11111111");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}