📄 user.java
字号:
package com.suninformation.user;
import com.suninformation.database.*;
import com.suninformation.tools.CheckValue;
import java.sql.*;
/**
* @author 刘镇
*
* 用户类
*/
public class User {
private static final String INSERT_USER = "INSERT INTO puser(username,password,papertype,papernumber,usertype,regtime) VALUES(?,?,?,?,?,?)";
private static final String SAVE_USER = "UPDATE puser SET password=?,securitied=?,dataisfull=?,usertype=?,lastlogintime=?,lastloginip=?,logincount=?,online=? WHERE userName=?";
private static final String LOAD_USER_BY_USERNAME = "SELECT userid,username,password,papertype,papernumber,securitied,dataisfull,usertype,regtime,lastlogintime,lastloginip,logincount,online FROM puser WHERE username=?";
private long userId = -1;
private String userName = null;
private String password = null;
private int paperType = -1;
private String paperNumber = null;
private int securitied = -1;
private int userType = -1;
private Date regTime = null;
private Date lastLoginTime = null;
private String lastLoginIp = null;
private int loginCount = -1;
private int onLine = -1;
private int dataIsFull;
private boolean isChanged = false;
public User(String userName, String password, int paperType,
String paperNumber) throws UserAlreadyExistsException,
UnacceptableException {
this.userName = userName;
this.password = CheckValue.md5(password);
this.paperType = paperType;
this.paperNumber = paperNumber;
this.securitied = 0;
this.dataIsFull = 0;
this.userType = USERTYPE.NORMAL;
this.regTime = new java.sql.Date(new java.util.Date().getTime());
insertIntoDB();
}
public User(String userName) throws UserNotFoundException,
UnacceptableException {
if (userName == null) {
throw new UnacceptableException("参数值有问题,“用户名”不能为空。");
}
this.userName = userName;
loadFromDB();
}
private void loadFromDB() throws UserNotFoundException,
UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(LOAD_USER_BY_USERNAME);
pstmt.setString(1, userName);
rs = pstmt.executeQuery();
if (!rs.next()) {
throw new UserNotFoundException("用户名为 " + userName + " 的用户不存在。");
}
this.userId = rs.getLong(1);
this.userName = rs.getString(2);
this.password = rs.getString(3);
this.paperType = rs.getInt(4);
this.paperNumber = rs.getString(5);
this.securitied = rs.getInt(6);
this.dataIsFull = rs.getInt(7);
this.userType = rs.getInt(8);
this.regTime = rs.getDate(9);
this.lastLoginTime = rs.getDate(10);
this.lastLoginIp = rs.getString(11);
this.loginCount = rs.getInt(12);
this.onLine = rs.getInt(13);
} catch (SQLException sqle) {
throw new UnacceptableException("读取用户数据失败。", sqle);
} finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
private void insertIntoDB() throws UserAlreadyExistsException,
UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
loadFromDB();
throw new UserAlreadyExistsException("该用户已存在!");
} catch (UserNotFoundException unfe) {
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(INSERT_USER);
pstmt.setString(1, userName);
pstmt.setString(2, password);
pstmt.setInt(3, paperType);
pstmt.setString(4, paperNumber);
pstmt.setInt(5, userType);
pstmt.setDate(6, regTime);
pstmt.executeUpdate();
} catch (SQLException e) {
throw new UnacceptableException("写入用户数据失败.", e);
} finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
}
private void saveToDB() throws UnacceptableException {
Connection conn = null;
try {
conn = DBManager.getConnection();
saveToDB(conn);
} catch (SQLException sqle) {
throw new UnacceptableException("保存数据出错", sqle);
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void saveToDB(Connection con) throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(SAVE_USER);
pstmt.setString(1, password);
pstmt.setInt(2, securitied);
pstmt.setInt(3, dataIsFull);
pstmt.setInt(4, userType);
pstmt.setDate(5, lastLoginTime);
pstmt.setString(6, lastLoginIp);
pstmt.setInt(7, loginCount);
pstmt.setInt(8, onLine);
pstmt.setString(9, userName);
pstmt.executeUpdate();
} finally {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////////
/**
* 获取用户ID(UserId)
*
* @return long
*/
public long getUserId() {
return userId;
}
/**
* 获取用户名称(UserName)
*
* @return String
*/
public String getUserName() {
return userName;
}
/**
* 获取用户密码(Password)
*
* @return String
*/
public String getPassword() {
return password;
}
/**
* 设置用户密码(Password)
*
* @param password
* String
*/
public void setPassword(String password) {
this.password = CheckValue.md5(password);
this.isChanged = true;
}
/**
* 获取证件类型(PaperType)
*
* @return int
*/
public int getPaperType() {
return paperType;
}
/**
* 获取证件号码(PaperNumber)
*
* @return String
*/
public String getPaperNumber() {
return paperNumber;
}
/**
* 获取“用户是否设置了安全信息值”(Securitied)
*
* @return int 0-没有设置,1-已设置
*/
public int getSecuritied() {
return securitied;
}
/**
* 设置“用户是否设置了安全信息值”(Securitied)
*
* @param securitied
* int 0/1
*/
public void setSecuritied(int securitied) {
this.securitied = securitied;
this.isChanged = true;
}
/**
* 获取“用户个人信息是否填写完全”(DataIsFull)
*
* @return int
*/
public int getDataIsFull() {
return dataIsFull;
}
/**
* 设置“用户个人信息是否填写完全”(DataIsFull)
*
* @param dataIsFull
* int 0/1
*/
public void setDataIsFull(int dataIsFull) {
this.dataIsFull = dataIsFull;
this.isChanged = true;
}
/**
* 获取用户类型(UserType)
*
* @return int
*/
public int getUserType() {
return userType;
}
/**
* 设置用户类型(UserType)
*
* @param userType
* int
*/
public void setUserType(int userType) {
this.userType = userType;
this.isChanged = true;
}
/**
* 获取用户注册时间(RegTime)
*
* @return Date
*/
public Date getRegTime() {
return regTime;
}
/**
* 获取用户最后登陆时间(LastLoginTime)
*
* @return Date
*/
public Date getLastLoginTime() {
return lastLoginTime;
}
/**
* 设置用户最后登陆时间(LastLoginTime)
*
* @param lastLoginTime
* Date
*/
public void setLastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
this.isChanged = true;
}
/**
* 获取用户最后登陆时IP地址(LastLoginIp)
*
* @return String
*/
public String getLastLoginIp() {
return lastLoginIp;
}
/**
* 设置用户最后登陆时IP地址(LastLoginIp)
*
* @param lastLoginIp
* String
*/
public void setLastLoginIp(String lastLoginIp) {
this.lastLoginIp = lastLoginIp;
this.isChanged = true;
}
/**
* 获取用户登陆次数(LoginCount)
*
* @return int
*/
public int getLoginCount() {
return loginCount;
}
/**
* 设置用户登陆次数(LoginCount)
*
* @param loginCount
* int
*/
public void setLoginCount(int loginCount) {
this.loginCount = loginCount;
this.isChanged = true;
}
/**
* 获取用户是否在线(OnLine)
*
* @return int 0-不在,1-在
*/
public int getOnLine() {
return onLine;
}
/**
* 设置用户是否在线(OnLine)
*
* @param onLine
* int
*/
public void setOnLine(int onLine) {
this.onLine = onLine;
this.isChanged = true;
}
/**
* 保存信息修改;当对该类的属性进行更改后,可以使用该方法进行写入数据库操作。
*
*/
public void save() {
if (this.isChanged) {
try {
saveToDB();
} catch (UnacceptableException ue) {
}
}
}
/**
* 获取用户信息操作实例
*
* @return userInfo
* @throws UserNotFoundException
* @throws UnacceptableException
*/
public UserInfo getUserInfo() throws UserNotFoundException,
UnacceptableException {
UserInfo us = null;
if (userName != null) {
us = new UserInfo(userName);
}
return us;
}
/**
* 获取用户安全信息操作实例
*
* @return securityInfo
* @throws UserNotFoundException
* @throws UnacceptableException
*/
public SecurityInfo getSecurityInfo() throws UserNotFoundException,
UnacceptableException {
SecurityInfo si = null;
if (userName != null) {
si = new SecurityInfo(userName);
}
return si;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -