?? 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 + -