package cn.bbs.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 cn.bbs.dao.UserDao;
import cn.bbs.model.UserModel;
import cn.bbs.util.DbUtil;
public class UserDaoImpl implements UserDao {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
boolean flag = false;
UserModel user = null;
List<UserModel> list = null;
int total = 0;
//登录底层数据交互
@Override
public boolean login(UserModel user) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement("select * from lt_user where user_name=? and user_password=? and user_state=0");
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
rs = ps.executeQuery();
if(rs.next()){
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return flag;
}
//根据用户名获取用户信息底层数据交互
@Override
public UserModel getUserByname(String s) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement("select * from lt_user where user_name=? and user_state=0");
ps.setString(1,s);
rs = ps.executeQuery();
while(rs.next()){
user = new UserModel();
user.setId(rs.getInt("id"));
user.setResour(rs.getInt("user_resour"));
user.setUsername(rs.getString("user_name"));
user.setPassword(rs.getString("user_password"));
user.setState(rs.getInt("user_state"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return user;
}
//注册底层数据交互
@Override
public boolean register(UserModel user) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement("insert into lt_user values(0,?,?,1,0)");
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
int n = ps.executeUpdate();
if(n>0){
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return flag;
}
//判断用户名是否存在底层数据交互
@Override
public boolean getUserByname1(String s) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement("select * from lt_user where user_name=? ");
ps.setString(1,s);
rs = ps.executeQuery();
if(rs.next()){
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return flag;
}
//查询用户总数
@Override
public int usertotal(String sql){
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return total;
}
//用户列表
@Override
public List<UserModel> userlist(String sql) {
conn = DbUtil.getConnection();
list =new ArrayList<UserModel>();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
user = new UserModel();
user.setId(rs.getInt("id"));
user.setResour(rs.getInt("user_resour"));
user.setUsername(rs.getString("user_name"));
user.setPassword(rs.getString("user_password"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return list;
}
//刪除用戶
@Override
public boolean delete(int id) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement("update lt_user set user_state=1 where id=?");
ps.setInt(1, id);
int n = ps.executeUpdate();
if(n>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return flag;
}
//通过ID得到用户
@Override
public UserModel getUserByid(int id) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement("select * from lt_user where id=? and user_state=0");
ps.setInt(1,id);
rs = ps.executeQuery();
while(rs.next()){
user = new UserModel();
user.setId(rs.getInt("id"));
user.setResour(rs.getInt("user_resour"));
user.setUsername(rs.getString("user_name"));
user.setPassword(rs.getString("user_password"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return user;
}
//修改用户
@Override
public boolean editUser(UserModel user) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement("update lt_user set user_password=? where id=?");
ps.setString(1, user.getPassword());
ps.setInt(2, user.getId());
int n = ps.executeUpdate();
if(n>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return flag;
}
//模糊查找用户
@Override
public UserModel getUser(String sql) {
conn = DbUtil.getConnection();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
user = new UserModel();
user.setId(rs.getInt("id"));
user.setResour(rs.getInt("user_resour"));
user.setUsername(rs.getString("user_name"));
user.setPassword(rs.getString("user_password"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DbUtil.close(conn, ps, rs);
}
return user;
}
}