📄 usermessage.java
字号:
package com.suninformation.message;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.suninformation.user.UnacceptableException;
import com.suninformation.database.DBManager;
/**
* @author 刘镇
*
* 用户个人消息类
*/
public class UserMessage {
private static final String INSERT_USERMESSAGE = "INSERT INTO pusermessage(formusername,tousername,subject,content,msgtype) VALUES(?,?,?,?,?)";
private static final String SAVE_USERMESSAGE = "UPDATE pusermessage SET isNew=?,isRe=? WHERE msgid=?";
private static final String LOAD_USERMESSAGE_BY_MSGID = "SELECT msgid,fromusername,tousername,subject,content,msgtype,isNew,isRe,sendtime FROM pusermessage WHERE msgid=?";
private long msgId = -1;
private String fromUserName = null;
private String toUserName = null;
private String subject = null;
private String content = null;
private int msgType = -1;
private int isNew = -1;
private int isRe = -1;
private Date sendTime = null;
public UserMessage(String fromUserName, String toUserName, String subject,
String content, int msgType) throws UnacceptableException {
this.fromUserName = fromUserName;
this.toUserName = toUserName;
this.subject = subject;
this.content = content;
this.msgType = msgType;
this.sendTime = new java.sql.Date(new java.util.Date().getTime());
insertIntoDB();
}
public UserMessage(long msgId) throws UnacceptableException {
if (msgId < 1) {
throw new UnacceptableException("您输入的参数值有问题,userName不能为null。");
}
this.msgId = msgId;
loadFromDB();
}
private void loadFromDB() throws UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(LOAD_USERMESSAGE_BY_MSGID);
pstmt.setLong(1, msgId);
rs = pstmt.executeQuery();
if (!rs.next()) {
throw new UnacceptableException("短信息记录不存在。");
}
this.msgId = rs.getLong(1);
this.fromUserName = rs.getString(2);
this.toUserName = rs.getString(3);
this.subject = rs.getString(4);
this.content = rs.getString(5);
this.msgType = rs.getInt(6);
this.isNew = rs.getInt(7);
this.isRe = rs.getInt(8);
this.sendTime = rs.getDate(9);
} catch (SQLException sqle) {
throw new UnacceptableException("从数据库中取用户短信息数据失败。", sqle);
} finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
private void insertIntoDB() throws UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(INSERT_USERMESSAGE);
pstmt.setString(1, fromUserName);
pstmt.setString(2, toUserName);
pstmt.setString(3, subject);
pstmt.setString(4, content);
pstmt.setInt(5, msgType);
pstmt.executeUpdate();
} catch (SQLException e) {
throw new UnacceptableException("向数据库中写入用户短信息数据失败.", e);
} finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
private void saveToDB() {
Connection conn = null;
try {
conn = DBManager.getConnection();
saveToDB(conn);
} catch (Exception sqle) {
sqle.printStackTrace(); //出错
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void saveToDB(Connection con) throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(SAVE_USERMESSAGE);
pstmt.setInt(1, isNew);
pstmt.setInt(2, isRe);
pstmt.setLong(3, msgId);
pstmt.executeUpdate();
} finally {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////////
/**
* 消息内容
*
* @return String
* @todo Implement this com.suninformation.message.userMessage method
*/
public String getContent() {
return content;
}
/**
* 发件人用户名
*
* @return String
* @todo Implement this com.suninformation.message.userMessage method
*/
public String getFromUserName() {
return fromUserName;
}
/**
* 是否为新消息
*
* @return int
* @todo Implement this com.suninformation.message.userMessage method
*/
public int getIsNew() {
return isNew;
}
/**
* 是否回复过
*
* @return int
* @todo Implement this com.suninformation.message.userMessage method
*/
public int getIsRe() {
return isRe;
}
/**
* 消息ID
*
* @return long
* @todo Implement this com.suninformation.message.userMessage method
*/
public long getMsgId() {
return msgId;
}
/**
* 发送时间
*
* @return Date
* @todo Implement this com.suninformation.message.userMessage method
*/
public Date getSendTime() {
return sendTime;
}
/**
* 消息标题
*
* @return String
* @todo Implement this com.suninformation.message.userMessage method
*/
public String getSubject() {
return subject;
}
/**
* 收件人用户名
*
* @return String
* @todo Implement this com.suninformation.message.userMessage method
*/
public String getToUserName() {
return toUserName;
}
public int getMsgType() {
return msgType;
}
/**
* setIsNew
*
* @param isNew
* int
* @todo Implement this com.suninformation.message.userMessage method
*/
public void setIsNew(int isNew) {
this.isNew = isNew;
saveToDB();
}
/**
* setIsRe
*
* @param isRe
* int
* @todo Implement this com.suninformation.message.userMessage method
*/
public void setIsRe(int isRe) {
this.isRe = isRe;
saveToDB();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -