kaoqindb.java
来自「一个用java编写的功能强大的OA系统」· Java 代码 · 共 341 行
JAVA
341 行
package com.redmoon.oa.kaoqin;import java.sql.*;import java.util.*;import cn.js.fan.base.*;import cn.js.fan.db.*;import cn.js.fan.util.*;public class KaoqinDb extends ObjectDb { private int id; public static final int TYPE_SYSTEM = 1; public static final int TYPE_USER = 0; public KaoqinDb() { init(); } public KaoqinDb(int id) { this.id = id; init(); load(); } public int getId() { return id; } public void initDB() { tableName = "kaoqin"; primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT); objectCache = new KaoqinCache(this); isInitFromConfigDB = false; QUERY_CREATE = "insert into " + tableName + " (name,reason,direction,type,myDate) values (?,?,?,?,NOW())"; QUERY_SAVE = "update " + tableName + " set name=?,reason=?,direction=?,type=? where id=?"; QUERY_LIST = "select id from " + tableName + " order by mydate desc"; QUERY_DEL = "delete from " + tableName + " where id=?"; QUERY_LOAD = "select name,reason,direction,type,myDate from " + tableName + " where id=?"; } public KaoqinDb getKaoqinDb(int id) { return (KaoqinDb)getObjectDb(new Integer(id)); } public boolean isKaoqinDone(String name, String direction, String type) { boolean re = false; Calendar cal = Calendar.getInstance(); int myhour = cal.get(cal.HOUR_OF_DAY); String fh = "<"; if (myhour < 12) fh = "<"; else fh = ">"; String sql = "select id from kaoqin where name=? and direction=? and type=?"; sql += " and (TO_DAYS(NOW()) - TO_DAYS(myDate))=0 and HOUR(NOW())" + fh + "12 and HOUR(myDate)" + fh + "12"; Conn conn = new Conn(connname); try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, direction); ps.setString(3, type); ResultSet rs = conn.executePreQuery(); if (rs != null && rs.next()) { re = true; } } catch (SQLException e) { logger.error("isKaoqinDone:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return re; } public boolean create() throws ErrMsgException { Conn conn = new Conn(connname); boolean re = false; try { PreparedStatement ps = conn.prepareStatement(QUERY_CREATE); ps.setString(1, name); ps.setString(2, reason); ps.setString(3, direction); ps.setString(4, type); re = conn.executePreUpdate()==1?true:false; if (re) { KaoqinCache rc = new KaoqinCache(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) { KaoqinCache rc = new KaoqinCache(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 KaoqinDb(pk.getIntValue()); } 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()) { name = rs.getString(1); reason = rs.getString(2); direction = rs.getString(3); type = rs.getString(4); myDate = rs.getTimestamp(5); 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.setString(1, name); ps.setString(2, reason); ps.setString(3, direction); ps.setString(4, type); ps.setInt(5, id); re = conn.executePreUpdate()==1?true:false; if (re) { KaoqinCache rc = new KaoqinCache(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 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(getKaoqinDb(rs.getInt(1))); } } } catch (SQLException e) { logger.error("list:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return result; } 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 { KaoqinDb ug = getKaoqinDb(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 String getName() { return name; } public String getReason() { return reason; } public java.util.Date getMyDate() { return myDate; } public String getDirection() { return direction; } public void setName(String name) { this.name = name; } public void setReason(String reason) { this.reason = reason; } public void setMyDate(java.util.Date myDate) { this.myDate = myDate; } public void setType(String type) { this.type = type; } public String getType() { return type; } public void setDirection(String direction) { this.direction = direction; } private String name; private String reason; private java.util.Date myDate; private String type; private String direction;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?