📄 officedb.java
字号:
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.*;public class OfficeDb extends ObjectDb { private int id; public static final int TYPE_PUBLIC = 1; public static final int TYPE_USER = 0; public OfficeDb() { init(); try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public OfficeDb(int id) { this.id = id; init(); load(); } public int getId() { return id; } public int getTypeId() { return typeId; } public String getOfficeName() { return officeName; } public double getPrice() { return price; } public String getBuyPerson() { return buyPerson; } public int getStorageCount() { return storageCount; } public String getMeasureUnit() { return measureUnit; } public java.util.Date getBuyDate() { return buyDate; } public String getAbstracts() { return abstracts; } public void initDB() { tableName = "office_equipment"; primaryKey = new PrimaryKey("id", PrimaryKey.TYPE_INT); objectCache = new OfficeCache(this); isInitFromConfigDB = false; QUERY_CREATE = "insert into " + tableName + " (typeId, officeName,price,buyPerson,storageCount,measureUnit,buyDate,abstracts) values (?,?,?,?,?,?,?,?)"; QUERY_SAVE = "update " + tableName + " set typeId=?, officeName=?,price=?,buyPerson=?,storageCount=?,measureUnit=?,buyDate=?,abstracts=? where id=?"; QUERY_LIST = "select id from " + tableName; QUERY_DEL = "delete from " + tableName + " where id=?"; QUERY_LOAD = "select typeId, officeName,price,buyPerson,storageCount,measureUnit,buyDate,abstracts from " + tableName + " where id=?"; } public OfficeDb getOfficeDb(int id) { return (OfficeDb) 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, typeId); ps.setString(2, officeName); ps.setDouble(3, price); ps.setString(4, buyPerson); ps.setInt(5, storageCount); ps.setString(6, measureUnit); if (buyDate != null) ps.setDate(7, new java.sql.Date(buyDate.getTime())); else ps.setDate(7, null); ps.setString(8,abstracts); re = conn.executePreUpdate() == 1 ? true : false; if (re) { OfficeCache rc = new OfficeCache(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) { OfficeCache rc = new OfficeCache(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 OfficeDb(pk.getIntValue()); } public boolean hasOfficeOfType(int typeId) { ResultSet rs = null; Conn conn = new Conn(connname); try { String sql = "select id from office_equipment where typeId=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, typeId); 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()) { typeId = rs.getInt(1); officeName = rs.getString(2); price = rs.getDouble(3); buyPerson = rs.getString(4); storageCount = rs.getInt(5); measureUnit = rs.getString(6); try{ buyDate = rs.getDate(7); }catch(Exception e){ logger.error("load1:" + e.getMessage()); } abstracts = rs.getString(8); 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, typeId); ps.setString(2, officeName); ps.setDouble(3, price); ps.setString(4, buyPerson); ps.setInt(5, storageCount); ps.setString(6, measureUnit); if (buyDate==null) ps.setDate(7, null); else ps.setDate(7, new java.sql.Date(buyDate.getTime())); ps.setString(8,abstracts); ps.setInt(9,id); re = conn.executePreUpdate() == 1 ? true : false; if (re) { OfficeCache rc = new OfficeCache(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 { OfficeDb ug = getOfficeDb(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(getOfficeDb(rs.getInt(1))); } } } catch (SQLException e) { logger.error("list:" + e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return result; } private int typeId; private String officeName; private double price; private String buyPerson; private int storageCount; private String measureUnit; private java.util.Date buyDate; private String abstracts; private void jbInit() throws Exception { } public void setTypeId(int typeId) { this.typeId = typeId; } public void setOfficeName(String officeName) { this.officeName = officeName; } public void setPrice(double price) { this.price = price; } public void setBuyPerson(String buyPerson) { this.buyPerson = buyPerson; } public void setStorageCount(int storageCount) { this.storageCount = storageCount; } public void setMeasureUnit(String measureUnit) { this.measureUnit = measureUnit; } public void setBuyDate(java.util.Date buyDate) { this.buyDate = buyDate; } public void setId(int id) { this.id = id; } public void setAbstracts(String abstracts) { this.abstracts = abstracts; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -