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

📄 messagedb.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
字号:
package cn.js.fan.module.guestbook;

import java.sql.*;

import cn.js.fan.base.ObjectDb;
import cn.js.fan.db.Conn;
import cn.js.fan.db.PrimaryKey;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import cn.js.fan.web.Global;
import org.apache.log4j.Logger;
import cn.js.fan.util.DateUtil;

public class MessageDb extends ObjectDb {
    String connname = Global.defaultDB;
    String userName;
    private String reply;
    String ip;
    int id;

    Logger logger = Logger.getLogger(MessageDb.class.getName());

    public MessageDb() {
        super();
    }

    public MessageDb(int id) {
        this.id = id;
        load();
        init();
    }

    public int getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getUserName() {
        return userName;
    }

    public java.util.Date getDate() {
        return lydate;
    }

    public String getReply() {
        return reply;
    }

    public String getShopCode() {
        return shopCode;
    }

    public java.util.Date getLydate() {
        return lydate;
    }

    public java.util.Date getRedate() {
        return redate;
    }

    public java.util.Date getReDate() {
        return redate;
    }

    public void initDB() {
        this.tableName = "guestbook";
        primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT);
        objectCache = new MessageCache(this);

        this.QUERY_DEL =
                "delete FROM guestbook WHERE id=?";
        this.QUERY_CREATE =
                "INSERT INTO guestbook (content, userName, ip, lydate, shopCode) VALUES (?,?,?,?,?)";
        this.QUERY_LOAD =
                "SELECT id, content, userName, ip, lydate, reply, redate, shopCode FROM guestbook WHERE id=?";
        this.QUERY_SAVE =
                "UPDATE guestbook SET content=?, reply=?, redate=? WHERE id=?";
        isInitFromConfigDB = false;
    }

    public boolean save() throws ErrMsgException {
        // Based on the id in the object, get the message data from the database.
        Conn conn = new Conn(connname);
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(this.QUERY_SAVE);
            pstmt.setString(1, content);
            pstmt.setString(2, reply);
            pstmt.setString(3, "" + System.currentTimeMillis());
            pstmt.setInt(3, id);
            if (conn.executePreUpdate() == 1) {
                MessageCache mc = new MessageCache(this);
                primaryKey.setValue(new Integer(id));
                mc.refreshSave(primaryKey);
                return true;
            }
            else
                return false;
        } catch (SQLException e) {
            logger.error(e.getMessage());
            throw new ErrMsgException("更新留言出错!");
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
    }

    public void load() {
        // Based on the id in the object, get the message data from the database.
        Conn conn = new Conn(connname);
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(this.QUERY_LOAD);
            pstmt.setInt(1, id);
            rs = conn.executePreQuery();
            if (rs.next()) {
                this.id = rs.getInt(1);
                this.content = rs.getString(2);
                this.userName = rs.getString(3);
                this.ip = rs.getString(4);
                try {
                    lydate = DateUtil.parse(rs.getString(5));
                }
                catch (Exception e) {

                }
                this.reply = StrUtil.getNullString(rs.getString(6));
                String str = rs.getString(7);
                if (str!=null) {
                    try {
                        redate = DateUtil.parse(rs.getString(7));
                    }
                    catch (Exception e) {

                    }
                }
                shopCode = rs.getString(8);

                primaryKey.setValue(new Integer(id));
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {}
                rs = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
    }

    public boolean del() throws
            ErrMsgException {
        Conn conn = null;
        ResultSet rs = null;
        try {
            conn = new Conn(connname);
            PreparedStatement pstmt = conn.prepareStatement(this.QUERY_DEL);
            pstmt.setInt(1, id);
            if (conn.executePreUpdate()==1) {
                MessageCache mc = new MessageCache(this);
                primaryKey.setValue(new Integer(id));
                mc.refreshDel(primaryKey);
                return true;
            }
            else
                return false;
        } catch (SQLException e) {
            logger.error(e.getMessage());
            throw new ErrMsgException("删除出错!");
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
    }

    public boolean create() throws ErrMsgException {
        Conn conn = null;
        ResultSet rs = null;
        try {
            conn = new Conn(connname);
            PreparedStatement pstmt = conn.prepareStatement(this.QUERY_CREATE);
            pstmt.setString(1, content);
            pstmt.setString(2, userName);
            pstmt.setString(3, ip);
            pstmt.setString(4, "" + System.currentTimeMillis());
            pstmt.setString(5, shopCode);
            conn.executePreUpdate();

            MessageCache mc = new MessageCache(this);
            mc.refreshCreate();
        } catch (SQLException e) {
            logger.error("create:" + e.getMessage());
            throw new ErrMsgException("插入留言出错!");
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return true;
    }

    public MessageDb getMessageDb(int id) {
        return new MessageDb(id);
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setReply(String reply) {
        this.reply = reply;
    }

    public void setShopCode(String shopCode) {
        this.shopCode = shopCode;
    }

    public void setLydate(Date lydate) {
        this.lydate = lydate;
    }

    public void setRedate(Date redate) {
        this.redate = redate;
    }

    public ObjectDb getObjectRaw(PrimaryKey pk) {
        return new MessageDb(pk.getIntValue());
    }

    private String content;
    private String shopCode = "default";
    private java.util.Date lydate;
    private java.util.Date redate;
}

⌨️ 快捷键说明

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