package com.dw.dao.impl;
/**
* 数据访问层,名片信息CRUD操作
* @author DY1101shaoyuxian
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.dw.dao.MingpianDao;
import com.dw.model.Mingpian;
import com.dw.util.DbConn;
public class MingpianDaoImpl implements MingpianDao {
private Connection conn = DbConn.getConn();
/**
* 获取指定的用戶(更新页面操作使用)
*
* @param id
* @return stu
*/
public Mingpian findmingpianByid(int id,String state) {
Mingpian stu = null;
String sql = "select * from mingpian where stId=? and state=?";
try {
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
psmt.setString(2, state);
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
int stId = rs.getInt("stId");
String stName = rs.getString("stName");
String stSex = rs.getString("stSex");
String stAge = rs.getString("stAge");
Long stTel = rs.getLong("stTel");
String stDept = rs.getString("stDept");
String stAddress = rs.getString("stAddress");
String username = rs.getString("username");
String type = rs.getString("type");
stu = new Mingpian(stId, stName, stSex, stAge, stTel, stDept,
stAddress,state,username,type);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
/**
* 添加名片信息
*
* @param stu
* @retursn flag
*/
public boolean addmingpian(Mingpian stu) {
// mingpian(stId,stName,stSex,stAge,stTel,stDept,stAddress)
boolean flag = false;
String sql = "insert into mingpian(stId,stName,stSex,stAge,stTel,stDept,stAddress,state,username,type) values(?,?,?,?,?,?,?,?,?,?)";
try {
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, stu.getStId());
psmt.setString(2, stu.getStName());
psmt.setString(3, stu.getStSex());
psmt.setString(4, stu.getStAge());
psmt.setLong(5, stu.getStTel());
psmt.setString(6, stu.getStDept());
psmt.setString(7, stu.getStAddress());
psmt.setString(8, "1");
psmt.setString(9, stu.getUsername());
psmt.setString(10, stu.getType());
int i = psmt.executeUpdate();
if (i == 1) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
/**
* 删除名片信息
*
* @param id
* @return flag
*/
public boolean delmingpian(int id) {
boolean flag = false;
String sql = "delete from mingpian where stId=?";
try {
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
if (psmt.executeUpdate() > 0) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
/**
* 更新名片信息
*
* @param stu
* @return flag
*/
public boolean updatemingpian(Mingpian stu) {
boolean flag = false;
String sql = "update mingpian set stName=?,stSex=?,stAge=?,stTel=?,stDept=?,stAddress=? where stId=? ";
try {
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1, stu.getStName());
psmt.setString(2, stu.getStSex());
psmt.setString(3, stu.getStAge());
psmt.setLong(4, stu.getStTel());
psmt.setString(5, stu.getStDept());
psmt.setString(6, stu.getStAddress());
psmt.setInt(7, stu.getStId());
int i = psmt.executeUpdate();
if (i == 1) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
/**
* 查询全体名片信息
*
* @return list
*/
@SuppressWarnings("unchecked")
public List StSelect(String username,String type) {
List list = new ArrayList();
String sql = "";
if("1".equals(type)){
sql = "select * from mingpian where state='1'";
}else{
sql = "select * from mingpian where state='1' and username='"+username+"'";
}
try {
Statement smt = conn.createStatement();
ResultSet rs = smt.executeQuery(sql);
// mingpian(stId,stName,stSex,stAge,stTel,stDept,stAddress)
while (rs.next()) {
int stId = rs.getInt("stId");
String stName = rs.getString("stName");
String stSex = rs.getString("stSex");
String stAge = rs.getString("stAge");
Long stTel = rs.getLong("stTel");
String stDept = rs.getString("stDept");
String stAddress = rs.getString("stAddress");
String state=rs.getString("state");
Mingpian stu = new Mingpian(stId, stName, stSex, stAge, stTel,
stDept, stAddress,state,username,type);
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public List StHuiShou(String username,String type) {
List list = new ArrayList();
String sql = "";
if("1".equals(type)){
sql = "select * from mingpian where state='0'";
}else{
sql = "select * from mingpian where state='0' and username='"+username+"'";
}
try {
Statement smt = conn.createStatement();
ResultSet rs = smt.executeQuery(sql);
// mingpian(stId,stName,stSex,stAge,stTel,stDept,stAddress)
while (rs.next()) {
int stId = rs.getInt("stId");
String stName = rs.getString("stName");
String stSex = rs.getString("stSex");
String stAge = rs.getString("stAge");
Long stTel = rs.getLong("stTel");
String stDept = rs.getString("stDept");
String stAddress = rs.getString("stAddress");
String state=rs.getString("state");
Mingpian stu = new Mingpian(stId, stName, stSex, stAge, stTel,
stDept, stAddress,state,username,type);
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public List findmingpianById(String id,String state,String username,String type) {
// TODO Auto-generated method stub
Mingpian mingpian = null;
List list = new ArrayList();
String sql = "";
if("1".equals(type)){
sql = "select * from mingpian where stId like '%"+id+"%' and state='"+state+"'";
}else{
sql = "select * from mingpian where stId like '%"+id+"%' and state='"+state+"' and username='"+username+"'";
}
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
mingpian = new Mingpian();
mingpian.setStId(Integer.parseInt(rs.getString("stId")));
mingpian.setStName(rs.getString("stName"));
mingpian.setStSex(rs.getString("stSex"));
mingpian.setStAge(rs.getString("stAge"));
mingpian.setStDept(rs.getString("stDept"));
mingpian.setStAddress(rs.getString("stAddress"));
mingpian.setStTel(Long.parseLong(rs.getString("stTel")));
mingpian.setState(rs.getString("state"));
list.add(mingpian);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public boolean UpdateStateByID(int d, String state) {
boolean flag = false;
String sql = "update mingpian set state=? where stId=? ";
try {
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1, state);
psmt.setInt(2, d);
int i = psmt.executeUpdate();
if (i == 1) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public void insertBatch(List<Mingpian> list) {
for(int m=0;m<list.size();m++){
Mingpian stu=list.get(m);
String sql = "insert into mingpian(stId,stName,stSex,stAge,stTel,stDept,stAddress,state,username,type) values(?,?,?,?,?,?,?,?,?,?)";
try {
PreparedStatement psmt = conn.prepareStatement(sql);
psm