⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smmember.java

📁 计算机技术的快速发展
💻 JAVA
字号:
/*
 * 创建日期 2005-5-6
 */
package com.suninformation.schoolmate;

import java.sql.*;

import com.suninformation.database.*;
import com.suninformation.user.*;

/**
 * @author 刘镇
 *
 * 校友录班级成员类
 */
public class SMMember {

    private static final String LOAD_MEMBER_BY_USERNAME_CLASSID = "SELECT membertype,ispassed,memberid,logincount,lastlogintime,lastloginip FROM sm_member WHERE username=? AND classid=?";

    private static final String LOAD_MEMBER_BY_MEMBERID = "SELECT membertype,ispassed,memberid,username,classid,logincount,lastlogintime,lastloginip FROM sm_member WHERE memberid=?";

    private static final String INSERT_MEMBER = "INSERT INTO sm_member(username,classid,membertype,ispassed) VALUES(?,?,?,?)";

    private static final String SAVE_MEMBER = "UPDATE sm_member SET membertype=?,ispassed=?,logincount=?,lastlogintime=?,lastloginip=? WHERE username=? AND classid=?";

    private static final String MESSAGE_COUNT = "SELECT count(1) FROM sm_message WHERE username=? AND classid=?";

    private static final String DELETE_MEMBER_ALL_MESSAGE = "DELETE FROM sm_message WHERE username=? AND classid=?";

    private int memberId = -1;

    private String userName = null; //用户名

    private int memberType = -1; //成员类型:1-管理员、0-普通成员、2-班级友人

    private int classId = -1; //所属班级ID

    private int isPassed = -1; //是否已经被班级管理员批准

    private int LoginCount = -1; //登陆次数

    private Date LastLoginTime = null; //上次登陆时间

    private String LastLoginIp = null;//上次登陆IP

    private boolean isChanged = false; //已修改标记

    /**
     * @return 返回 isPassed。
     */
    public int getIsPassed() {
        return isPassed;
    }

    /**
     * @param isPassed
     *            要设置的 isPassed。
     */
    public void setIsPassed(int isPassed) {
        this.isPassed = isPassed;
        this.isChanged = true;
    }

    /**
     * @return 返回 memberType。
     */
    public int getMemberType() {
        return memberType;
    }

    /**
     * @param memberType
     *            要设置的 memberType。
     */
    public void setMemberType(int memberType) {
        this.memberType = memberType;
        this.isChanged = true;
    }

    /**
     * @return 返回 classId。
     */
    public int getClassId() {
        return classId;
    }

    /**
     * @return 返回 userName。
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @return int
     */
    public int getMemberId() {
        return memberId;
    }

    /**
     * @return 返回 lastLoginTime。
     */
    public Date getLastLoginTime() {
        return LastLoginTime;
    }

    /**
     * @param lastLoginTime
     *            要设置的 lastLoginTime。
     */
    public void setLastLoginTime(Date lastLoginTime) {
        LastLoginTime = lastLoginTime;
        this.isChanged = true;
    }

    /**
     * @return 返回 loginCount。
     */
    public int getLoginCount() {
        return LoginCount;
    }

    /**
     * @param loginCount
     *            要设置的 loginCount。
     */
    public void setLoginCount(int loginCount) {
        LoginCount = loginCount;
        this.isChanged = true;
    }

    /**
     * @return 返回 lastLoginIp。
     */
    public String getLastLoginIp() {
        return LastLoginIp;
    }

    /**
     * @param lastLoginIp
     *            要设置的 lastLoginIp。
     */
    public void setLastLoginIp(String lastLoginIp) {
        LastLoginIp = lastLoginIp;
        this.isChanged = true;
    }

    /////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////

    /**
     * 构造方法,通过username获取班级classid中的成员
     *
     * @param username
     * @throws SMMemberNotFoundException
     * @throws UnacceptableException
     */
    public SMMember(String username, int classid)
            throws SMMemberNotFoundException, UnacceptableException {
        this.userName = username;
        this.classId = classid;
        loadFromDB();
    }

    /**
     * 构造方法,通过memberId获取成员信息
     *
     * @param memberid
     *            int
     * @throws SMMemberNotFoundException
     * @throws UnacceptableException
     */
    public SMMember(int memberid) throws SMMemberNotFoundException,
            UnacceptableException {
        this.memberId = memberid;
        loadFromDB();
    }

    /**
     * 构造方法,把用户username添加到班级classid,成员类型为membertype
     *
     * @param username
     * @param membertype
     * @param classid
     * @throws SMMemberAlreadyExistsException
     * @throws UnacceptableException
     */
    public SMMember(String username, int membertype, int classid, int ispassed)
            throws SMMemberAlreadyExistsException, UnacceptableException {
        this.userName = username;
        this.memberType = membertype;
        this.classId = classid;
        this.isPassed = ispassed;
        insertIntoDB();
    }

    /**
     * 获取该成员信息
     *
     * @param username
     *            用户名
     * @return UserInfo类实例
     * @throws UserNotFoundException
     * @throws UnacceptableException
     */
    public UserInfo getUserInfo() throws UserNotFoundException,
            UnacceptableException {
        return new UserInfo(this.userName);
    }

    /**
     * 读取成员信息
     *
     * @throws SMMemberNotFoundException
     * @throws UnacceptableException
     */
    private void loadFromDB() throws SMMemberNotFoundException,
            UnacceptableException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBManager.getConnection();
            if (this.userName != null || this.userName != ""
                    && this.classId != -1) {
                pstmt = conn.prepareStatement(LOAD_MEMBER_BY_USERNAME_CLASSID);
                pstmt.setString(1, this.userName);
                pstmt.setInt(2, this.classId);
                rs = pstmt.executeQuery();
                if (!rs.next()) {
                    throw new SMMemberNotFoundException("班级" + this.classId
                            + "成员“" + this.userName + "”不存在。");
                }
                this.memberType = rs.getInt(1);
                this.isPassed = rs.getInt(2);
                this.memberId = rs.getInt(3);
                this.LoginCount = rs.getInt(4);
                this.LastLoginTime = rs.getDate(5);
                this.LastLoginIp = rs.getString(6);
            } else {
                pstmt = conn.prepareStatement(LOAD_MEMBER_BY_MEMBERID);
                pstmt.setInt(1, this.memberId);
                rs = pstmt.executeQuery();
                if (!rs.next()) {
                    throw new SMMemberNotFoundException("班级" + this.classId
                            + "成员“" + this.userName + "”不存在。");
                }
                this.memberType = rs.getInt(1);
                this.isPassed = rs.getInt(2);
                this.memberId = rs.getInt(3);
                this.userName = rs.getString(4);
                this.classId = rs.getInt(5);
                this.LoginCount = rs.getInt(6);
                this.LastLoginTime = rs.getDate(7);
                this.LastLoginIp = rs.getString(8);
            }
        } catch (SQLException sqle) {
            throw new UnacceptableException("读取成员数据失败。", sqle);
        } finally {
            DBManager.closeObject(conn, pstmt, rs);
        }
    }

    /**
     * 添加新成员操作
     *
     * @throws SMMemberAlreadyExistsException
     * @throws UnacceptableException
     */
    private void insertIntoDB() throws SMMemberAlreadyExistsException,
            UnacceptableException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            loadFromDB();
            throw new SMMemberAlreadyExistsException("该成员已存在!");
        } catch (SMMemberNotFoundException unfe) {
            try {
                conn = DBManager.getConnection();
                pstmt = conn.prepareStatement(INSERT_MEMBER);
                pstmt.setString(1, this.userName);
                pstmt.setInt(2, this.classId);
                pstmt.setInt(3, this.memberType);
                pstmt.setInt(4, this.isPassed);
                pstmt.executeUpdate();
            } catch (SQLException e) {
                throw new UnacceptableException("写入成员数据失败.", e);
            } catch (UnacceptableException eee) {
                throw new UnacceptableException("写入成员数据失败.", eee);
            } 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_MEMBER);
            pstmt.setInt(1, this.memberType);
            pstmt.setInt(2, this.isPassed);
            pstmt.setInt(3, this.LoginCount);
            pstmt.setDate(4, this.LastLoginTime);
            pstmt.setString(5, this.LastLoginIp);
            pstmt.setString(6, this.userName);
            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 message
     * @param msgtype
     * @throws SMMessageAlreadyExistsException
     * @throws UnacceptableException
     */
    public void sendMessage(String message, int msgtype)
            throws SMMessageAlreadyExistsException, UnacceptableException {
        new SMMessage(message, msgtype, this.userName, this.classId);
    }

    /**
     * 获取该用户发表的留言数量
     *
     * @return
     * @throws UnacceptableException
     */
    public int getMessageCount() throws UnacceptableException {
        int count = -1;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBManager.getConnection();
            pstmt = conn.prepareStatement(MESSAGE_COUNT);
            pstmt.setString(1, this.userName);
            pstmt.setInt(2, this.classId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException sqle) {

        } finally {
            DBManager.closeObject(conn, pstmt, rs);
        }
        return count;
    }

    /**
     * 删除该用户发表的所有留言
     *
     * @throws UnacceptableException
     */
    public void deleteMessages() throws UnacceptableException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBManager.getConnection();
            pstmt = conn.prepareStatement(DELETE_MEMBER_ALL_MESSAGE);
            pstmt.setString(1, this.userName);
            pstmt.setInt(2, this.classId);
            pstmt.executeUpdate();
        } catch (SQLException sqle) {
            throw new UnacceptableException("删除用户留言失败!", sqle);
        } finally {
            DBManager.closeObject(conn, pstmt, rs);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -