package com.sl.dao;
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 com.sl.entity.User;
import com.sl.util.ConnectionFactory;
import com.sl.util.ResourceClose;
public class UserDaoImpl implements UserDao{
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
public int addUser(User user) {
conn=ConnectionFactory.getConnection();
int num=0;
try {
String sql="insert into t_users values(null,?,?,?,?,?,?,?,?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,user.getUsername());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getName());
pstmt.setString(4,user.getNic());
pstmt.setString(5,user.getSex());
pstmt.setInt(6,user.getAge());
pstmt.setString(7,user.getEmail());
pstmt.setString(8,user.getPhone());
pstmt.setString(9,user.getSelfshow());
pstmt.setInt(10,user.getRoleId());
num=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return num;
}
public boolean login(User user) {
conn=ConnectionFactory.getConnection();
boolean flag=false;
try {
String sql="select * from t_users where username=? and password=?";
pstmt=conn.prepareStatement("select * from t_users where username=? and password=?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2,user.getPassword());
rs=pstmt.executeQuery();
if(rs.next())
{
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setName(rs.getString(4));
user.setNic(rs.getString(5));
user.setSex(rs.getString(6));
user.setAge(rs.getInt(7));
user.setEmail(rs.getString(8));
user.setPhone(rs.getString(9));
user.setSelfshow(rs.getString(10));
user.setRoleId(rs.getInt(11));
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return flag;
}
public User selectUserInfo(int id) {
User user=new User();
conn=ConnectionFactory.getConnection();
try {
String sql="select * from t_users where id=?";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
while(rs.next())
{
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setName(rs.getString(4));
user.setNic(rs.getString(5));
user.setSex(rs.getString(6));
user.setAge(rs.getInt(7));
user.setEmail(rs.getString(8));
user.setPhone(rs.getString(9));
user.setSelfshow(rs.getString(10));
user.setRoleId(rs.getInt(11));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return user;
}
public List findAllUsers() {
List userList=new ArrayList();
conn=ConnectionFactory.getConnection();
try {
String sql="select * from t_users where roleid=0";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
User user=new User();
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setName(rs.getString(4));
user.setNic(rs.getString(5));
user.setSex(rs.getString(6));
user.setAge(rs.getInt(7));
user.setEmail(rs.getString(8));
user.setPhone(rs.getString(9));
user.setSelfshow(rs.getString(10));
user.setRoleId(rs.getInt(11));
userList.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return userList;
}
/**
* 删除用户
*/
public int deleteUser(int id) {
// TODO Auto-generated method stub
conn=ConnectionFactory.getConnection();
int count=0;
try {
pstmt=conn.prepareStatement("delete from t_users where id=?");
pstmt.setInt(1,id);
count=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return count;
}
/**
* 查找所有管理员信息
*/
public List findAllAdmins() {
// TODO Auto-generated method stub
List adminList=new ArrayList();
conn=ConnectionFactory.getConnection();
try {
pstmt=conn.prepareStatement("select * from t_users where roleid=1");
rs=pstmt.executeQuery();
while(rs.next())
{
User user=new User();
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setName(rs.getString(4));
user.setNic(rs.getString(5));
user.setSex(rs.getString(6));
user.setAge(rs.getInt(7));
user.setEmail(rs.getString(8));
user.setPhone(rs.getString(9));
user.setSelfshow(rs.getString(10));
user.setRoleId(rs.getInt(11));
adminList.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return adminList;
}
/**
* 查找新增用户
*/
public List findNewUsers() {
List newuserList=new ArrayList();
conn=ConnectionFactory.getConnection();
try {
String sql="select * from t_users where roleid=0 order by id desc limit 5";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
User user=new User();
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setName(rs.getString(4));
user.setNic(rs.getString(5));
user.setSex(rs.getString(6));
user.setAge(rs.getInt(7));
user.setEmail(rs.getString(8));
user.setPhone(rs.getString(9));
user.setSelfshow(rs.getString(10));
user.setRoleId(rs.getInt(11));
newuserList.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return newuserList;
}
/**
* 修改用户信息
*/
public int modifyUserInfo(User user) {
// TODO Auto-generated method stub
conn=ConnectionFactory.getConnection();
int num=0;
try {
pstmt=conn.prepareStatement("update t_users set username=?,password=?,name=?,nic=?,sex=?,age=?,email=?,"
+"phone=?,selfshow=?,roleid=? where id=?");
pstmt.setString(1,user.getUsername());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getName());
pstmt.setString(4,user.getNic());
pstmt.setString(5,user.getSex());
pstmt.setInt(6,user.getAge());
pstmt.setString(7,user.getEmail());
pstmt.setString(8,user.getPhone());
pstmt.setString(9,user.getSelfshow());
pstmt.setInt(10,user.getRoleId());
pstmt.setInt(11,user.getId());
num=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
ResourceClose.close(rs, pstmt, conn);
}
return num;
}
public int getUserid(String us