📄 smclass.java
字号:
/*
* 创建日期 2005-5-5
*/
package com.suninformation.schoolmate;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.suninformation.database.DBManager;
import com.suninformation.user.UnacceptableException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
/**
* @author 刘镇
*
* 校友录班级类
*/
public class SMClass {
private static final String LOAD_CLASS_BY_CLASSID = "SELECT schoolid,classname,beginyear,initiator,createdate,classpronunciamento FROM sm_class WHERE classid=?";
private static final String LOAD_CLASS_BY_CLASSNAME = "SELECT schoolid,classid,beginyear,initiator,createdate,classpronunciamento FROM sm_class WHERE classname=?";
private static final String INSERT_CLASS = "INSERT INTO sm_class(classname,schoolid,beginyear,createdate,initiator) VALUES(?,?,?,?,?)";
private static final String SAVE_CLASS = "UPDATE sm_class SET classname=?,schoolid=?,beginyear=?,initiator=?,createdate=?,classpronunciamento=? WHERE classid=?";
private static final String MEMBER_COUNT =
"SELECT count(1) FROM sm_member WHERE classid=?";
private static final String ALL_MEMBER_FOR_THIS_CLASS =
"SELECT username FROM sm_member WHERE classid=?";
private static final String SEARCH_MEMBER_FROM_THIS_CLASS = "SELECT username FROM sm_member WHERE schoolid=? AND beginyear=? AND classname LIKE %?% ORDER BY classname";
private static final String MESSAGE_COUNT =
"SELECT count(1) FROM sm_message WHERE classid=?";
private static final String ALL_MESSAGE_FOR_THIS_CLASS =
"SELECT msgid FROM sm_message WHERE classid=? ORDER BY msgid DESC, modifyTime DESC";
private static final String DELETE_MEMBER =
"DELETE FROM sm_member WHERE username=? AND classid=?";
private static final String DELETE_ALL_MEMBER =
"DELETE FROM sm_member WHERE classid=?";
private static final String DELETE_MESSAGE =
"DELETE FROM sm_message WHERE msgid=?";
private static final String DELETE_ALL_MESSAGE =
"DELETE FROM sm_message WHERE classid=?";
private int classId = -1; //班级ID
private int schoolId = -1; //所属学校ID
private String className = null; //班级名称
private String beginYear = null; //入学年份
private String initiator = null; //创始人用户名
private Date createDate = null; //创建日期
private String classPronunciamento = null; //班级宣言
private boolean isChanged = false; //已修改标记
/**
* 构造方法,其可以通过classid读取班级信息。
*
* @param classid int
* @throws SMClassNotFoundException
* @throws UnacceptableException
*/
public SMClass(int classid) throws SMClassNotFoundException,
UnacceptableException {
this.classId = classid;
loadFromDB();
}
/**
* 构造方法,用来为该学校创建一个新的班级。
*
* @param schoolid int
* @param classname String
* @param beginyear String
* @param username String
* @throws SMClassAlreadyExistsException
* @throws UnacceptableException
*/
public SMClass(int schoolid, String classname, String beginyear,
String username) throws SMClassAlreadyExistsException,
UnacceptableException {
this.className = classname;
this.schoolId = schoolid;
this.beginYear = beginyear;
this.createDate = new Date(System.currentTimeMillis());
this.initiator = username;
insertIntoDB();
try {
loadFromDB();
SMMember smm = new SMMember(username, 1, this.classId, 1);
}
catch (Exception ee) {
}
}
/**
* 读取班级信息
*
* @throws SMClassNotFoundException
* @throws UnacceptableException
*/
private void loadFromDB() throws SMClassNotFoundException,
UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
if (this.classId == -1) {
pstmt = conn.prepareStatement(LOAD_CLASS_BY_CLASSNAME);
pstmt.setString(1, this.className);
rs = pstmt.executeQuery();
if (!rs.next()) {
throw new SMClassNotFoundException("班级“" + this.className
+ "”不存在。");
}
this.schoolId = rs.getInt(1);
this.classId = rs.getInt(2);
this.beginYear = rs.getString(3);
this.initiator = rs.getString(4);
this.createDate = rs.getDate(5);
this.classPronunciamento = rs.getString(6);
}
else {
pstmt = conn.prepareStatement(LOAD_CLASS_BY_CLASSID);
pstmt.setInt(1, this.classId);
rs = pstmt.executeQuery();
if (!rs.next()) {
throw new SMClassNotFoundException("班级ID“" + this.classId
+ "”不存在。");
}
this.schoolId = rs.getInt(1);
this.className = rs.getString(2);
this.beginYear = rs.getString(3);
this.initiator = rs.getString(4);
this.createDate = rs.getDate(5);
this.classPronunciamento = rs.getString(6);
}
}
catch (SQLException sqle) {
throw new UnacceptableException("读取班级数据失败。", sqle);
}
finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
/**
* 添加新班级操作
*
* @throws SMClassAlreadyExistsException
* @throws UnacceptableException
*/
private void insertIntoDB() throws SMClassAlreadyExistsException,
UnacceptableException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
loadFromDB();
throw new SMClassAlreadyExistsException("该班级已存在!");
}
catch (SMClassNotFoundException unfe) {
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(INSERT_CLASS);
pstmt.setString(1, this.className);
pstmt.setInt(2, this.schoolId);
pstmt.setString(3, this.beginYear);
pstmt.setDate(4, this.createDate);
pstmt.setString(5, this.initiator);
pstmt.executeUpdate();
}
catch (SQLException e) {
throw new UnacceptableException("写入班级数据失败.", e);
}
finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
}
/**
* 保存数据到DB
*
* @throws UnacceptableException
*/
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 Connection
* @throws SQLException
*/
private void saveToDB(Connection con) throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(SAVE_CLASS);
pstmt.setString(1, this.className);
pstmt.setInt(2, this.schoolId);
pstmt.setString(3, this.beginYear);
pstmt.setDate(4, this.createDate);
pstmt.setString(5, this.initiator);
pstmt.setString(6, this.classPronunciamento);
pstmt.setInt(7, this.classId);
pstmt.executeUpdate();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 保存信息修改;当对该类的属性进行更改后,可以使用该方法进行写入数据库操作。
*
*/
public void save() {
if (this.isChanged) {
try {
saveToDB();
}
catch (UnacceptableException ue) {
}
}
}
/**
* 添加班级成员
*
* @param username String
* @param membertype int
* @return com.suninformation.schoolmate.SMMember
* @throws SMMemberAlreadyExistsException
* @throws UnacceptableException
*/
public SMMember addNewSMMember(String username, int membertype) throws
SMMemberAlreadyExistsException, UnacceptableException {
return new SMMember(username, membertype, this.classId, 0);
}
/**
* 删除班级成员username
*
* @param username String
* @throws SMMemberNotFoundException
* @throws UnacceptableException
*/
public void deleteSMMember(String username) throws SMMemberNotFoundException,
UnacceptableException {
this.getSMMember(username).deleteMessages();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(DELETE_MEMBER);
pstmt.setString(1, username);
pstmt.setInt(2, this.classId);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
throw new UnacceptableException("删除班级成员失败!", sqle);
}
finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
/**
* 删除班级全部成员
*
* @throws SMMemberNotFoundException
* @throws UnacceptableException
*/
public void deleteSMMembers() throws SMMemberNotFoundException,
UnacceptableException {
this.deleteSMMessages(); //删除班级所有留言;然后下面代码为删除班级所有成员
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(DELETE_ALL_MEMBER);
pstmt.setInt(1, this.classId);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
throw new UnacceptableException("删除班级成员失败!", sqle);
}
finally {
DBManager.closeObject(conn, pstmt, rs);
}
}
/**
* 通过username获取该班的班级成员
*
* @param username String
* @return com.suninformation.schoolmate.SMMember
* @throws SMMemberNotFoundException
* @throws UnacceptableException
*/
public SMMember getSMMember(String username) throws SMMemberNotFoundException,
UnacceptableException {
return new SMMember(username, this.classId);
}
/**
* 获取该班级所拥有的成员总数
*
* @return int
* @throws UnacceptableException
*/
public int getSMMemberCount() throws UnacceptableException {
int count = -1;
Connection conn = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -