package com.operation;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import com.common.DBConnection;
import com.entity.User;
/*
* 用户对数据库操作方法管理类
*/
public class UserBo {
private Connection conn;// 保存数据库连接对象
private Statement stmt;// 用于执行SQL语句
private PreparedStatement psts;// 执行SQL语句(预处理)
private ResultSet rs;// 保存查询的结果集
/**
* 判断当前输入用户名和密码是否正确并封装user对象
*
* @param username
* @param password
* @return 用户对象 user
*/
public User validUser(String username, String password) {
User user = null;
String sql = "select * from user where username= ? and password= ?;";
try {
conn = DBConnection.getConnection();
psts = conn.prepareStatement(sql);
psts.setString(1, username);
psts.setString(2, password);
rs = psts.executeQuery();
if (rs.next()) {
user = new User();
user.setUserid(rs.getInt("userid"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setRealname(rs.getString("realname"));
user.setSex(rs.getString("sex"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(psts);
DBConnection.closeConnection();
}
return user;
}
/**
* 通过预处理语句进行用户的添加
*
* @param user
* @return count影响行数
*/
public int insertUser(User user) {
String sql = "insert into user(username,password,realname,sex) values(?,?,?,?) ";
int count = 0;
try {
conn = DBConnection.getConnection();
psts = conn.prepareStatement(sql);
psts.setString(1, user.getUsername());
psts.setString(2, user.getPassword());
psts.setString(3, user.getRealname());
psts.setString(4, user.getSex());
count = psts.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeStatement(psts);
DBConnection.closeConnection();
}
return count;
}
/**
* 根据传入的页面数和页面大小对页面进行分页并将user对象添加到链表中
*
* @param pagesize
* @param pagecount
* @return list链表
*/
public List selectAllUser(int pagesize, int pagecount) {
List list = new ArrayList();
String sql = "select * from user limit " + (pagesize - 1) * pagecount
+ "," + pagecount;
// System.out.println(sql);
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
User user = new User();
user.setUserid(rs.getInt("userid"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setRealname(rs.getString("realname"));
user.setSex(rs.getString("sex"));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(stmt);
DBConnection.closeConnection();
}
return list;
}
/**
* 根据通讯录userid删除相关信息
*
* @param userid
* @return 影响数据库行数
*/
public int deleteUserByUserid(String userid) {
String sql = "delete from user where userid= ? ";
int count = 0;
try {
conn = DBConnection.getConnection();
psts = conn.prepareStatement(sql);
psts.setString(1, userid);
// System.out.println(sql);
count = psts.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeStatement(psts);
DBConnection.closeConnection();
}
return count;
}
/**
* 根据用户名对密码进行更新
* @param username
* @param password1
* @return
*/
public int UpdateUserByUserid(String username, String password1) {
String sql = "update user set password='" + password1
+ "' where username = '" + username + "';";
int count = 0;
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
count = stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeStatement(stmt);
DBConnection.closeConnection();
}
return count;
}
/**
* 通过对数据库查询页数
* @return 页面总页数
*/
public int getAllnum() {
int totalPages = 0;
String sql = "select count(*) from user";
conn = DBConnection.getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
rs.next();
totalPages = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return totalPages;
}
/**
* 通过查询用户名来判断用户是否存在
* @param username
* @return
*/
public boolean voidUser(String username) {
boolean flag = false;
String sql = "select * from user where username='" + username + "'";
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(stmt);
DBConnection.closeConnection();
}
return flag;
}
/**
* 通过用户名和密码来更新用户的密码
* @param username
* @param password
* @return
*/
public boolean voidUser(String username, String password) {
//System.out.println("sss" + username + "我进来了" + password);
boolean flag = false;
String sql = "select * from user where username='" + username
+ "'and password='" + password + "';";
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeResultSet(rs);
DBConnection.closeStatement(stmt);
DBConnection.closeConnection();
}
return flag;
}
}