📄 smmessage.java
字号:
/*
* 创建日期 2005-5-8
*/
package com.suninformation.schoolmate;
import java.sql.*;
import com.suninformation.database.*;
import com.suninformation.user.*;
/**
* @author 刘镇
*
* 班级留言类
*/
public class SMMessage {
private static final String LOAD_MESSAGE_BY_MSGID = "SELECT username,messagecontent,messagetype,classid,sendtime,modifytime FROM sm_message WHERE msgid=?";
private static final String INSERT_MESSAGE = "INSERT INTO sm_message(username,classid,messagecontent,messagetype,modifytime,sendtime) VALUES(?,?,?,?,?,?)";
private static final String SAVE_MESSAGE = "UPDATE sm_message SET messagecontent=?,messagetype=?,modifytime=? WHERE msgid=?";
private int msgId = -1; //留言ID
private String userName = null; //用户名
private String messageContent = null; //留言内容
private int messageType = -1; //留言类型:0-普通文本;1-多彩文本;2-系统信息;3-...
private int classId = -1; //所属班级ID
private Date sendTime = null; //发表时间
private Date modifyTime = null; //修改时间
private boolean isChanged = false; //已修改标记
/**
* @return 返回 messageContent。
*/
public String getMessageContent() {
return messageContent;
}
/**
* @param messageContent
* 要设置的 messageContent。
*/
public void setMessageContent(String messageContent) {
this.messageContent = messageContent;
this.isChanged = true;
}
/**
* @return 返回 messageType。
*/
public int getMessageType() {
return messageType;
}
/**
* @param messageType
* 要设置的 messageType。
*/
public void setMessageType(int messageType) {
this.messageType = messageType;
this.isChanged = true;
}
/**
* @return 返回 modifyTime。
*/
public Date getModifyTime() {
return modifyTime;
}
/**
* @param modifyTime
* 要设置的 modifyTime。
*/
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
this.isChanged = true;
}
/**
* @return 返回 userName。
*/
public String getUserName() {
return userName;
}
/**
* @return 返回 classId。
*/
public int getClassId() {
return classId;
}
/**
* @return 返回 sendTime。
*/
public Date getSendTime() {
return sendTime;
}
public int getMsgId() {
return msgId;
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
public SMMessage(int msgid) throws SMMessageNotFoundException,
UnacceptableException {
this.msgId = msgid;
loadFromDB();
}
public SMMessage(String msgcontent, int msgtype, String username,
int classid) throws SMMessageAlreadyExistsException,
UnacceptableException {
this.messageContent = msgcontent;
this.messageType = msgtype;
this.userName = username;
this.classId = classid;
this.sendTime = new Date(System.currentTimeMillis());
this.modifyTime = this.sendTime;
insertIntoDB();
}
/**
* 读取班级留言信息
*
* @throws SMMessageNotFoundException
* @throws UnacceptableException
*/
private void loadFromDB() throws SMMessageNotFoundException,
UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(LOAD_MESSAGE_BY_MSGID);
pstmt.setInt(1, this.msgId);
rs = pstmt.executeQuery();
if (!rs.next()) {
throw new SMMessageNotFoundException("班级" + this.classId
+ "留言“" + this.msgId + "”不存在。");
}
this.userName = rs.getString(1);
this.messageContent = rs.getString(2);
this.messageType = rs.getInt(3);
this.classId = rs.getInt(4);
this.sendTime = rs.getDate(5);
this.modifyTime = rs.getDate(6);
}
catch (SQLException sqle) {
throw new UnacceptableException("读取成员数据失败。", sqle);
}
finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
/**
* 添加新成员操作
*
* @throws SMMessageAlreadyExistsException
* @throws UnacceptableException
*/
private void insertIntoDB() throws SMMessageAlreadyExistsException,
UnacceptableException {
/* try {
SMMessage[] smm = (new SMClass(this.classId)).getSMMeaages();
if (smm != null) {
for (int i = 0; i < smm.length; i++) {
if (this.messageContent.equals(smm[i].getMessageContent()) && this.userName.equals(smm[i].getUserName())) {
throw new SMMessageAlreadyExistsException(
"留言已存在!");
}
}
}
}
catch (SMClassNotFoundException smce) {
throw new UnacceptableException("没有找到参数所指定的班级ID", smce);
}*/
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(INSERT_MESSAGE);
pstmt.setString(1, this.userName);
pstmt.setInt(2, this.classId);
pstmt.setString(3, this.messageContent);
pstmt.setInt(4, this.messageType);
pstmt.setDate(5, this.modifyTime);
pstmt.setDate(6, this.sendTime);
pstmt.executeUpdate();
}
catch (SQLException e) {
throw new UnacceptableException("写入留言数据失败.", e);
}
finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
/**
* 保存数据到DB
*
*/
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();
}
}
}
/**
* 保存数据库到DB
*
* @param con
* @throws SQLException
*/
private void saveToDB(Connection con) throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(SAVE_MESSAGE);
pstmt.setString(1, this.messageContent);
pstmt.setInt(2, this.messageType);
pstmt.setDate(3, this.modifyTime);
pstmt.setInt(4, this.msgId);
pstmt.executeUpdate();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 保存信息修改;当对该类的属性进行更改后,可以使用该方法进行写入数据库操作。
*
*/
public void save() {
if (this.isChanged) {
try {
this.modifyTime = new Date(System.currentTimeMillis());
saveToDB();
}
catch (UnacceptableException ue) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -