package com.group1.chenfeng.impl;
import java.io.File;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.group1.chenfeng.dao.InfoDao;
import com.group1.chenfeng.pojo.Info;
import com.group1.chenfeng.pojo.PageBean;
import com.group1.chenfeng.util.ConnectionUtil;
public class InfoDaoImpl implements InfoDao {
ConnectionUtil cu = new ConnectionUtil();
Connection conn = null;
Statement st = null;
PreparedStatement psmt = null;
ResultSet rs = null;
public boolean deleteInfo(Info info) {
boolean flag = false;
String sql = "delete from info where id='" + info.getId() + "'";
try {
conn = cu.getConnection();
st = conn.createStatement();
flag = st.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
cu.closeResource(conn, st, rs);
}
return flag;
}
public Info findInfoById(int id) {
Info info = new Info();
String sql = "select * from info where id='" + id + "'";
try {
conn = cu.getConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
if (rs.next()) {
info.setId(rs.getInt("id"));
info.setName(rs.getString("name"));
info.setSex(rs.getInt("sex"));
info.setBirthday(rs.getDate("birthday"));
info.setAddress_postcode(rs.getString("address_postcode"));
info.setBlood_type(rs.getInt("blood_type"));
info.setEmail(rs.getString("email"));
info.setEyesight(rs.getString("eyesight"));
info.setHealth(rs.getString("health"));
info.setHome_phone(rs.getString("home_phone"));
info.setIdcode(rs.getString("idcode"));
info.setIllness(rs.getInt("illness"));
info.setMarried(rs.getInt("married"));
info.setMobilephone(rs.getString("mobilephone"));
info.setNative_place(rs.getString("native_place"));
info.setNation(rs.getString("nation"));
info.setPolity_visage(rs.getString("polity_visage"));
info.setRegisted_place(rs.getString("registed_place"));
info.setResidence(rs.getInt("residence"));
info.setStature(rs.getInt("stature"));
info.setWeight(rs.getInt("weight"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
cu.closeResource(conn, st, rs);
}
return info;
}
public boolean insertInfo(Info info) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
boolean flag = false;
int count = 0;
StringBuffer sql = new StringBuffer();
sql.append("insert into info(name,sex,nation,birthday,native_place,polity_visage,married,residence,"
+ "health,blood_type,stature,illness,eyesight,weight,registed_place,idcode,address_postcode,home_phone,"
+ "email,mobilephone)values('");
sql.append(info.getName());
sql.append("','");
sql.append(info.getSex());
sql.append("','");
sql.append(info.getNation());
sql.append("','");
sql.append(Date.valueOf(sdf.format(info.getBirthday()).toString()));
sql.append("','");
sql.append(info.getNative_place());
sql.append("','");
sql.append(info.getPolity_visage());
sql.append("','");
sql.append(info.getMarried());
sql.append("','");
sql.append(info.getResidence());
sql.append("','");
sql.append(info.getHealth());
sql.append("','");
sql.append(info.getBlood_type());
sql.append("','");
sql.append(info.getStature());
sql.append("','");
sql.append(info.getIllness());
sql.append("','");
sql.append(info.getEyesight());
sql.append("','");
sql.append(info.getWeight());
sql.append("','");
sql.append(info.getRegisted_place());
sql.append("','");
sql.append(info.getIdcode());
sql.append("','");
sql.append(info.getAddress_postcode());
sql.append("','");
sql.append(info.getHome_phone());
sql.append("','");
sql.append(info.getEmail());
sql.append("','");
sql.append(info.getMobilephone());
sql.append("')");
conn = cu.getConnection();
try {
st = conn.createStatement();
count = st.executeUpdate(sql.toString());
if (count == 1) {
flag = true;
} else {
flag = false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
cu.closeResource(conn, st, rs);
}
return flag;
}
public boolean updateInfo(Info info) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
boolean flag = false;
String sql = "update info set name='" + info.getName() + "',sex='"
+ info.getSex() + "',nation='" + info.getNation()
+ "',birthday='"
+ Date.valueOf(sdf.format(info.getBirthday()).toString())
+ "',native_place='" + info.getNative_place()
+ "',polity_visage='" + info.getPolity_visage() + "',married='"
+ info.getMarried() + "',residence='" + info.getResidence()
+ "',health='" + info.getHealth() + "',blood_type='"
+ info.getBlood_type() + "',stature='" + info.getStature()
+ "',illness='" + info.getIllness() + "',eyesight='"
+ info.getEyesight() + "',weight='" + info.getWeight()
+ "',registed_place='" + info.getRegisted_place()
+ "',idcode='" + info.getIdcode() + "',address_postcode='"
+ info.getAddress_postcode() + "',home_phone='"
+ info.getHome_phone() + "',email='" + info.getEmail()
+ "',mobilephone='" + info.getMobilephone() + "'"
+ "where id='" + info.getId() + "'";
try {
conn = cu.getConnection();
st = conn.createStatement();
int count = st.executeUpdate(sql);
if (count == 1) {
flag = true;
} else {
flag = false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
cu.closeResource(conn, st, rs);
}
return flag;
}
public List<Info> getInfosPagenation(Info i, PageBean pageBean) {
List<Info> infos = new ArrayList<Info>();
String sql = null;
if (i == null) {
sql = "select * from info o order by o.id desc limit ?,? ";
} else {
if (i.getSex() != 0) {
sql = "select * from info o where o.sex='" + i.getSex()+ "' limit ?,?";
}
if (null != i.getName() && !"".equals(i.getName())) {
sql = "select * from info o where o.name like '%" + i.getName()+ "%' limit ?,?";
}
}
System.out.println("sql1.."+sql);
try {
conn = cu.getConnection();
psmt = conn.prepareStatement(sql);
psmt.setInt(2, pageBean.getPageSize());
psmt.setInt(1, (pageBean.getPageNo() - 1) * pageBean.getPageSize());
rs = psmt.executeQuery();
while (rs.next()) {
Info info = new Info();
info.setId(rs.getInt("id"));
info.setName(rs.getString("name"));
info.setSex(rs.getInt("sex"));
info.setBirthday(rs.getDate("birthday"));
info.setAddress_postcode(rs.getString("address_postcode"));
info.setBlood_type(rs.getInt("blood_type"));
info.setEmail(rs.getString("email"));
info.setEyesight(rs.getString("eyesight"));
info.setHealth(rs.getString("health"));
info.setHome_phone(rs.getString("home_phone"));
info.setIdcode(rs.getString("idcode"));
info.setIllness(rs.getInt("illness"));
info.setMarried(rs.getInt("married"));
info.setMobilephone(rs.getString("mobilephone"));
info.setNative_place(rs.getString("native_place"));
info.setNation(rs.getString("nation"));
info.setPolity_visage(rs.getString("polity_visage"));
info.setRegisted_place(rs.getString("registed_place"));
info.setResidence(rs.getInt("residence"));
info.setStature(rs.getInt("stature"));
info.setWeight(rs.getInt("weight"));
infos.add(info);
}
} catch (SQLException e) {
e.printStackTrace();
}
return infos;
}
public int getInfoCount(Info i) {
int count = 0;
String sql = "select count(*) as totalCount from info o";
if (i == null) {
sql = "select count(*) as totalCount from info";
}else{