📄 userinfo.java
字号:
package com.cattsoft.daoImpl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.cattsoft.DB.DBUtil;
import com.cattsoft.dao.UserInfoServer;
import com.cattsoft.vo.User;
public class UserInfo implements UserInfoServer {
private static final String SQL_ADD_USER = "insert into user(name, username, password, birthday, "
+ "position, hiredate, experience, major, sex, degree, role_id) values(?,?,?,?,?,?,?,?,?,?,?)";
private static final String SQL_QUERY_USER = "select * from user where username=?";
private static final String SQL_QUERY_BY_USER = "select count(*) from user where username=? and password=? and role_id=?";
private static final String SQL_QUERY_ALL_USER = "select * from user";
private static final String SQL_DELETE_USER = "delete from user where user_id = ?";
private static final String SQL_QUERY_BY_USERID = "select count(*) from user where username=?";
private static final String SQL_QUERY_ALL_EMPLOYEE = "select * from user where role_id = 'C'";
private static final String SQL_QUERY_ALL_MANAGER = "select user_id,name from user where role_id = 'B'";
private static final String SQL_UPDATE_USER = "update user set manager_name = ?,manager_id = ? where user_id = ?";
private static final String SQL_QUERY_BY = "select name from user where user_id = ?";
private static final String SQL_QUERY_NAME_BY = "select name from user where username = ?";
private static final String SQL_QUERY_USERID_BY = "select user_id from user where username = ?";
DBUtil db = new DBUtil(); // 实例化一个数据库连接对象
PreparedStatement pstmt = null; // 初始化数据库操作语句对象
ResultSet rs = null; // 初始化结果集对象
// 新增用户
public int addUser(User user) throws ClassNotFoundException, SQLException {
pstmt = db.getConnection().prepareStatement(SQL_ADD_USER);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getUsername());
pstmt.setString(3, user.getPassword());
pstmt.setString(4, user.getBirthday());
pstmt.setString(5, user.getPosition());
pstmt.setString(6, user.getHiredate());
pstmt.setString(7, user.getExperience());
pstmt.setString(8, user.getMajor());
pstmt.setString(9, user.getSex());
pstmt.setString(10, user.getDegree());
pstmt.setString(11, user.getRole_id());
int i = pstmt.executeUpdate();
pstmt.close();
db.connectionClose(db.getConnection());
return i;
}
// 根据用户名查询用户详细信息
public List<User> queryAll(String uname) throws ClassNotFoundException,
SQLException {
List<User> list = new ArrayList<User>();
pstmt = db.getConnection().prepareStatement(SQL_QUERY_USER);
pstmt.setString(1, uname);
rs = pstmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setUser_id(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setBirthday(rs.getString(4));
user.setPosition(rs.getString(5));
user.setHiredate(rs.getString(6));
user.setExperience(rs.getString(7));
user.setMajor(rs.getString(8));
user.setDegree(rs.getString(9));
user.setRole_id(rs.getString(10));
user.setSex(rs.getString(11));
user.setManager_name(rs.getString(12));
user.setName(rs.getString(13));
user.setManager_id(rs.getInt(14));
list.add(user);
}
rs.close();
pstmt.close();
db.connectionClose(db.getConnection());
return list;
}
// 根据用户名、密码、角色去查询用户信息
public boolean queryBy(String username, String password, String role_id)
throws ClassNotFoundException, SQLException {
pstmt = db.getConnection().prepareStatement(SQL_QUERY_BY_USER);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.setString(3, role_id);
rs = pstmt.executeQuery();
rs.next();
int n = rs.getInt(1);
if (n!=0) {
return true;
} else {
return false;
}
}
//查询所有人员信息
public List<User> queryAll() throws ClassNotFoundException, SQLException {
List<User> list = new ArrayList<User>();
pstmt = db.getConnection().prepareStatement(SQL_QUERY_ALL_USER);
rs = pstmt.executeQuery();
while(rs.next()){
User user = new User();
user.setUser_id(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setBirthday(rs.getString(4));
user.setPosition(rs.getString(5));
user.setHiredate(rs.getString(6));
user.setExperience(rs.getString(7));
user.setMajor(rs.getString(8));
user.setDegree(rs.getString(9));
user.setRole_id(rs.getString(10));
user.setSex(rs.getString(11));
user.setManager_name(rs.getString(12));
user.setName(rs.getString(13));
user.setManager_id(rs.getInt(14));
list.add(user);
}
rs.close();
pstmt.close();
db.connectionClose(db.getConnection());
return list;
}
//根据用户id删除对应的所有信息
public void deleteUser(int user_id) throws ClassNotFoundException,
SQLException {
pstmt = db.getConnection().prepareStatement(SQL_DELETE_USER);
pstmt.setInt(1,user_id);
pstmt.executeUpdate();
pstmt.close();
db.connectionClose(db.getConnection());
}
//根据用户名判断是否已经存在
public boolean queryBy(String uname) throws ClassNotFoundException,
SQLException {
pstmt = db.getConnection().prepareStatement(SQL_QUERY_BY_USERID);
pstmt.setString(1, uname);
rs = pstmt.executeQuery();
rs.next();
int n = rs.getInt(1);
if (n!=0) {
return true;
} else {
return false;
}
}
//查询所有员工信息
public List<User> queryAllEmployee() throws ClassNotFoundException,
SQLException {
List<User> list = new ArrayList<User>();
pstmt = db.getConnection().prepareStatement(SQL_QUERY_ALL_EMPLOYEE);
rs = pstmt.executeQuery();
while(rs.next()){
User user = new User();
user.setUser_id(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setBirthday(rs.getString(4));
user.setPosition(rs.getString(5));
user.setHiredate(rs.getString(6));
user.setExperience(rs.getString(7));
user.setMajor(rs.getString(8));
user.setDegree(rs.getString(9));
user.setRole_id(rs.getString(10));
user.setSex(rs.getString(11));
user.setManager_name(rs.getString(12));
user.setName(rs.getString(13));
user.setManager_id(rs.getInt(14));
list.add(user);
}
rs.close();
pstmt.close();
db.connectionClose(db.getConnection());
return list;
}
//查询出所有主管的id和姓名
public List<User> queryAllManager() throws ClassNotFoundException,
SQLException {
List<User> list = new ArrayList<User>();
pstmt = db.getConnection().prepareStatement(SQL_QUERY_ALL_MANAGER);
rs = pstmt.executeQuery();
while(rs.next()){
User user = new User();
user.setUser_id(rs.getInt(1));
user.setName(rs.getString(2));
list.add(user);
}
rs.close();
pstmt.close();
db.connectionClose(db.getConnection());
return list;
}
//根据用户id和主管id更新对应的用户信息
public void update(int user_id, int managerId ,String manager_name)
throws ClassNotFoundException, SQLException {
pstmt = db.getConnection().prepareStatement(SQL_UPDATE_USER);
pstmt.setString(1,manager_name);
pstmt.setInt(2,managerId);
pstmt.setInt(3,user_id);
pstmt.executeUpdate();
pstmt.close();
db.connectionClose(db.getConnection());
}
//根据主管id查询其姓名
public String queryBy(int managerId) throws ClassNotFoundException,
SQLException {
pstmt = db.getConnection().prepareStatement(SQL_QUERY_BY);
pstmt.setInt(1,managerId);
rs = pstmt.executeQuery();
User user = new User();
while(rs.next()){
user.setName(rs.getString(1));
}
rs.close();
pstmt.close();
db.connectionClose(db.getConnection());
return user.getName();
}
//根据用户名查询其姓名
public String queryNameBy(String username) throws ClassNotFoundException,
SQLException {
pstmt = db.getConnection().prepareStatement(SQL_QUERY_NAME_BY);
pstmt.setString(1,username);
rs = pstmt.executeQuery();
User user = new User();
while(rs.next()){
user.setName(rs.getString(1));
}
rs.close();
pstmt.close();
db.connectionClose(db.getConnection());
return user.getName();
}
//根据用户名查询其用户id
public int queryUserIdBy(String username) throws ClassNotFoundException,
SQLException {
pstmt = db.getConnection().prepareStatement(SQL_QUERY_USERID_BY);
pstmt.setString(1,username);
rs = pstmt.executeQuery();
User user = new User();
while(rs.next()){
user.setUser_id(rs.getInt(1));
}
rs.close();
pstmt.close();
db.connectionClose(db.getConnection());
return user.getUser_id();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -