📄 securityinfo.java
字号:
package com.suninformation.user;
import com.suninformation.database.*;
import com.suninformation.tools.CheckValue;
import java.sql.*;
/**
* @author 刘镇
*
* 用户安全信息类
*/
public class SecurityInfo {
private static final String INSERT_USER_SECURITY_INFO = "INSERT INTO psecurityinfo(username,securityemail,securitycode,question,answer,sendmailtime) VALUES(?,?,?,?,?,?)";
private static final String LOAD_USER_SECURITY_INFO_BY_USERNAME = "SELECT securityId,securityemail,securitycode,question,answer,sendmailtime FROM psecurityinfo WHERE username=?";
private static final String SAVE_USER_SECURITY_INFO = "UPDATE psecurityinfo SET question=?,answer=?,securityemail=?,sendmailtime=? WHERE username=?";
private long securityId = -1;
private String userName = null;
private String securityEmail = null;
private String securityCode = null;
private String question = null;
private String answer = null;
private Date sendMailTime = null;
private boolean isChanged = false;
public SecurityInfo(String userName, String securityEmail,
String securityCode, String question, String answer)
throws UserAlreadyExistsException, UnacceptableException {
this.userName = userName;
this.securityEmail = securityEmail;
this.securityCode = CheckValue.md5(securityCode);
this.question = question;
this.answer = answer;
insertIntoDB();
}
public SecurityInfo(String userName) throws UserNotFoundException,
UnacceptableException {
if (userName == null) {
throw new UnacceptableException("您输入的参数值有问题,userName不能为null。");
}
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_SECURITY_INFO_BY_USERNAME);
pstmt.setString(1, userName);
rs = pstmt.executeQuery();
if (!rs.next()) {
throw new UserNotFoundException(userName + " 的用户安全信息不存在。");
}
this.securityId = rs.getLong(1);
this.securityEmail = rs.getString(2);
this.securityCode = rs.getString(3);
this.question = rs.getString(4);
this.answer = rs.getString(5);
this.sendMailTime = rs.getDate(6);
} 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_SECURITY_INFO);
pstmt.setString(1, userName);
pstmt.setString(2, securityEmail);
pstmt.setString(3, securityCode);
pstmt.setString(4, question);
pstmt.setString(5, answer);
pstmt.setDate(6, sendMailTime);
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_SECURITY_INFO);
pstmt.setString(1, question);
pstmt.setString(2, answer);
pstmt.setString(3, securityEmail);
pstmt.setDate(4, sendMailTime);
pstmt.setString(5, userName);
pstmt.executeUpdate();
} finally {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////
/**
* getSecurityId
*
* @return long
*/
public long getSecurityId() {
return securityId;
}
/**
* getSecurityEmail
*
* @return String
*/
public String getSecurityEmail() {
return securityEmail;
}
/**
* setSecurityEmail
*
* @param securityEmail
* String
*/
public void setSecurityEmail(String securityEmail) {
this.securityEmail = securityEmail;
this.isChanged = true;
}
/**
* getSecurityCode
*
* @return String
*/
public String getSecurityCode() {
return securityCode;
}
/**
* getQuestion
*
* @return String
*/
public String getQuestion() {
return question;
}
/**
* setQuestion
*
* @param question
* String
*/
public void setQuestion(String question) {
this.question = question;
this.isChanged = true;
}
/**
* getAnswer
*
* @return String
*/
public String getAnswer() {
return answer;
}
/**
* setAnswer
*
* @param answer
* String
*/
public void setAnswer(String answer) {
this.answer = answer;
this.isChanged = true;
}
/**
* getSendMailTime
*
* @return Date
*/
public Date getSendMailTime() {
return sendMailTime;
}
/**
* setSendMailTime
*
* @param sendMailTime
* Date
*/
public void setSendMailTime(Date sendMailTime) {
this.sendMailTime = sendMailTime;
this.isChanged = true;
}
/**
* 保存信息修改;当对该类的属性进行更改后,可以使用该方法进行写入数据库操作。
*
*/
public void save() {
if (this.isChanged) {
try {
saveToDB();
} catch (UnacceptableException ue) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -