📄 usermessageproxy.java
字号:
package com.suninformation.message;
import com.suninformation.user.UnacceptableException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.suninformation.database.DBManager;
/**
* @author 刘镇
* @version 1.0
*
* 用户个人消息操作代理
*/
public class UserMessageProxy {
private static final String MSG_COUNT = "SELECT count(1) FROM pusermessage WHERE tousername=? AND isNew=?";
private static final String DELETE_MSG_BY_USERNAME = "DELETE FROM pusermessage WHERE tousername=?";
private static final String DELETE_MSG_BY_MSGID = "DELETE FROM pusermessage WHERE msgid=?";
private static final String ALL_MSGS = "SELECT msgid FROM pusermessage WHERE tousername=?";
/**
* 删除所有个人消息
*
* @param toUserName
* String
* @throws UnacceptableException
*/
public void deleteAllMessages(String toUserName)
throws UnacceptableException {
}
/**
* 删除个人消息msgId
*
* @param msgId
* long
* @throws UnacceptableException
*/
public void deleteMessage(long msgId) throws UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(DELETE_MSG_BY_MSGID);
pstmt.setLong(1, msgId);
pstmt.execute();
} catch (Exception e) {
throw new UnacceptableException("删除用户短信息数据失败!", e);
} finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
/**
* 获取个人消息
*
* @param msgId
* long
* @return userMessage
* @throws UnacceptableException
*/
public UserMessage getUserMessage(long msgId) throws UnacceptableException {
return new UserMessage(msgId);
}
/**
* 统计个人消息数量
*
* @param toUserName
* String
* @param isNew
* int
* @return int
* @throws UnacceptableException
*/
public int getUserMessageCount(String toUserName, int isNew)
throws UnacceptableException {
int count = -1;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(MSG_COUNT);
pstmt.setString(1, toUserName);
pstmt.setInt(2, isNew);
rs = pstmt.executeQuery();
if (rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException sqle) {
} finally {
DBManager.closeObject(conn, pstmt, rs);
}
return count;
}
/**
* 获取个人所有消息
*
* @param toUserName
* String
* @return userMessage[]
* @throws UnacceptableException
*/
public UserMessage[] getUserMessages(String toUserName)
throws UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
UserMessage[] iDBUserMessage = null;
int number = getUserMessageCount(toUserName, 0);
if (number >= 1) {
iDBUserMessage = new UserMessage[number];
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(ALL_MSGS);
rs = pstmt.executeQuery();
for (int i = 1; i <= number; i++) {
iDBUserMessage[i] = new UserMessage(rs.getLong(1));
}
} catch (Exception e) {
throw new UnacceptableException("数据库操作失败!", e);
} finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
return iDBUserMessage;
}
/**
* 发送个人消息
*
* @param fromUserName
* String
* @param toUserName
* String
* @param subject
* String
* @param content
* String
* @param msgType
* int
* @throws UnacceptableException
*/
public void sendMessage(String fromUserName, String toUserName,
String subject, String content, int msgType)
throws UnacceptableException {
new UserMessage(fromUserName, toUserName, subject, content, msgType);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -