officeopdb.java

来自「一个用java编写的功能强大的OA系统」· Java 代码 · 共 432 行

JAVA
432
字号
package com.redmoon.oa.officeequip;import java.sql.*;import java.util.*;import cn.js.fan.base.*;import cn.js.fan.db.*;import cn.js.fan.util.*;import com.redmoon.oa.person.UserDb;public class OfficeOpDb extends ObjectDb {    private int id;    public static final int TYPE_RECEIVE = 0;    public static final int TYPE_BORROW = 1;    public static final int TYPE_ADD = 2;    public OfficeOpDb() {        init();        try {            jbInit();        } catch (Exception ex) {            ex.printStackTrace();        }    }    public OfficeOpDb(int id) {        this.id = id;        init();        load();    }    public int getId() {        return id;    }    public int getOfficeId() {        return officeId;    }    public int getCount() {        return count;    }    public java.util.Date getOpDate() {        return opDate;    }    public int getType() {        return type;    }    public String getPerson() {        return person;    }    public String getRemark() {        return remark;    }    public java.util.Date getReturnDate() {        return returnDate;    }    public void initDB() {        tableName = "office_equipment_op";        primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT);        objectCache = new OfficeOpCache(this);        isInitFromConfigDB = false;        QUERY_CREATE =                "insert into " + tableName +                " (officeId, count,opDate,type,person,remark,returnDate) values (?,?,?,?,?,?,?)";        QUERY_SAVE = "update " + tableName + " set officeId=?, count=?,opDate=?,type=?,person=?,remark=?,returnDate=? where id=?";        QUERY_LIST =                "select id from " + tableName;        QUERY_DEL = "delete from " + tableName + " where id=?";        QUERY_LOAD =                "select officeId, count,opDate,type,person,remark,returnDate from " +                tableName + " where id=?";    }    public OfficeOpDb getOfficeOpDb(int id) {        return (OfficeOpDb) getObjectDb(new Integer(id));    }    public boolean create() throws ErrMsgException {        Conn conn = new Conn(connname);        boolean re = false;        try {                        PreparedStatement ps = conn.prepareStatement(QUERY_CREATE);            ps.setInt(1, officeId);            ps.setInt(2, count);            if (opDate != null) {                ps.setDate(3, new java.sql.Date(opDate.getTime()));            } else {                ps.setDate(3, null);            }            ps.setInt(4, type);            ps.setString(5, person);            UserDb ud = new UserDb();            ud = ud.getUserDb(person);            if (ud == null || !ud.isLoaded()) {                throw new ErrMsgException("用户" + person + "不存在!");            }            ps.setString(6, remark);            if (returnDate != null) {                ps.setDate(7, new java.sql.Date(returnDate.getTime()));            } else {                ps.setDate(7, null);            }            re = conn.executePreUpdate() == 1 ? true : false;            if (re) {                OfficeOpCache rc = new OfficeOpCache(this);                rc.refreshCreate();            }        } catch (SQLException e) {            logger.error("create:" + e.getMessage());            throw new ErrMsgException("数据库操作失败!");        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return re;    }        public boolean del() throws ErrMsgException {        Conn conn = new Conn(connname);        boolean re = false;        try {            PreparedStatement ps = conn.prepareStatement(QUERY_DEL);            ps.setInt(1, id);            re = conn.executePreUpdate() == 1 ? true : false;            if (re) {                OfficeOpCache rc = new OfficeOpCache(this);                primaryKey.setValue(new Integer(id));                rc.refreshDel(primaryKey);            }        } catch (SQLException e) {            logger.error("del: " + e.getMessage());        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return re;    }        public ObjectDb getObjectRaw(PrimaryKey pk) {        return new OfficeOpDb(pk.getIntValue());    }        public boolean hasOfficeOfType(int officeId) {        ResultSet rs = null;        Conn conn = new Conn(connname);        try {            String sql = "select id from office_equipment_op where officeId=?";            PreparedStatement ps = conn.prepareStatement(sql);            ps.setInt(1, officeId);            rs = conn.executePreQuery();            if (rs != null && rs.next()) {                return true;            }        } catch (SQLException e) {            logger.error("hasOfficeOfType: " + e.getMessage());        } finally {            if (rs != null) {                try {                    rs.close();                } catch (SQLException e) {}                rs = null;            }            if (conn != null) {                conn.close();                conn = null;            }        }        return false;    }        public void load() {        ResultSet rs = null;        Conn conn = new Conn(connname);        try {                        PreparedStatement ps = conn.prepareStatement(QUERY_LOAD);            ps.setInt(1, id);            rs = conn.executePreQuery();            if (rs != null && rs.next()) {                officeId = rs.getInt(1);                count = rs.getInt(2);                try {                    opDate = rs.getDate(3);                } catch (Exception e) {                    logger.error("load1:" + e.getMessage());                }                type = rs.getInt(4);                person = rs.getString(5);                remark = rs.getString(6);                try {                    returnDate = rs.getDate(3);                } catch (Exception e) {                    logger.error("load1:" + e.getMessage());                }                loaded = true;                primaryKey.setValue(new Integer(id));            }        } catch (SQLException e) {            logger.error("load: " + e.getMessage());        } finally {            if (rs != null) {                try {                    rs.close();                } catch (SQLException e) {}                rs = null;            }            if (conn != null) {                conn.close();                conn = null;            }        }    }        public boolean save() throws ErrMsgException {        Conn conn = new Conn(connname);        boolean re = false;        try {                        PreparedStatement ps = conn.prepareStatement(QUERY_SAVE);            ps.setInt(1, officeId);            ps.setInt(2, count);            if (opDate == null) {                ps.setDate(3, null);            } else {                ps.setDate(3, new java.sql.Date(opDate.getTime()));            }            ps.setInt(4, type);            ps.setString(5, person);            UserDb ud = new UserDb();            ud = ud.getUserDb(person);            ps.setString(6, remark);            if (returnDate == null) {                ps.setDate(7, null);            } else {                ps.setDate(7, new java.sql.Date(returnDate.getTime()));            }            ps.setInt(8, id);            re = conn.executePreUpdate() == 1 ? true : false;            if (re) {                OfficeOpCache rc = new OfficeOpCache(this);                primaryKey.setValue(new Integer(id));                rc.refreshSave(primaryKey);            }        } catch (SQLException e) {            logger.error("save: " + e.getMessage());        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return re;    }    public ListResult listResult(String listsql, int curPage, int pageSize) throws            ErrMsgException {        int total = 0;        ResultSet rs = null;        Vector result = new Vector();        ListResult lr = new ListResult();        lr.setTotal(total);        lr.setResult(result);        Conn conn = new Conn(connname);        try {                        String countsql = SQLFilter.getCountSql(listsql);            rs = conn.executeQuery(countsql);            if (rs != null && rs.next()) {                total = rs.getInt(1);            }            if (rs != null) {                rs.close();                rs = null;            }            if (total != 0) {                conn.setMaxRows(curPage * pageSize);             }            rs = conn.executeQuery(listsql);            if (rs == null) {                return lr;            } else {                rs.setFetchSize(pageSize);                int absoluteLocation = pageSize * (curPage - 1) + 1;                if (rs.absolute(absoluteLocation) == false) {                    return lr;                }                do {                    OfficeOpDb ug = getOfficeOpDb(rs.getInt(1));                    result.addElement(ug);                } while (rs.next());            }        } catch (SQLException e) {            logger.error(e.getMessage());            throw new ErrMsgException("数据库出错!");        } finally {            if (rs != null) {                try {                    rs.close();                } catch (Exception e) {}                rs = null;            }            if (conn != null) {                conn.close();                conn = null;            }        }        lr.setResult(result);        lr.setTotal(total);        return lr;    }        public boolean isExist(String tableName) {        ResultSet rs = null;        Conn conn = new Conn(connname);        try {            rs = conn.executeQuery(                    "select id from office_equipment where officeNum='" +                    tableName + "'");            if (rs.next()) {                return true;            }        } catch (SQLException e) {            logger.error("list:" + e.getMessage());        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return false;    }        public Vector list(String sql) {        ResultSet rs = null;        Conn conn = new Conn(connname);        Vector result = new Vector();        try {            rs = conn.executeQuery(sql);            if (rs == null) {                return null;            } else {                while (rs.next()) {                                        result.addElement(getOfficeOpDb(rs.getInt(1)));                }            }        } catch (SQLException e) {            logger.error("list:" + e.getMessage());        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return result;    }        private int officeId;    private int count;    private java.util.Date opDate;    private int type;    private String person;    private String remark;    private java.util.Date returnDate;    private void jbInit() throws Exception {    }    public void setOfficeId(int officeId) {        this.officeId = officeId;    }    public void setId(int id) {        this.id = id;    }    public void setOpDate(java.util.Date opDate) {        this.opDate = opDate;    }    public void setType(int type) {        this.type = type;    }    public void setPerson(String person) {        this.person = person;    }    public void setRemark(String remark) {        this.remark = remark;    }    public void setCount(int count) {        this.count = count;    }    public void setReturnDate(java.util.Date returnDate) {        this.returnDate = returnDate;    }}

⌨️ 快捷键说明

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