So how do you solve these problems? We can consider the use of Java object cache technology to achieve, that is, the code table is stored in the way of objects, the most easy to consider is the Map object, Map key is used to store code, value is used to store code objects, so that you can easily replace the code table through the code information.
In addition, the code table utility class does not need to create objects and be inherited, just need to get the Map object filled by the class name when the code initialization. The default constructor should be private. Other utility methods should be public static. There should also be a static code block that initializes the code table when the class is loaded.
Here’s an example: Suppose you have a stage code table that contains three attributes: code and name type. I’m now going to implement a phase code table utility class to demonstrate the above processing ideas.
* Specific code beans
* File: StageBean.java
* User: leizhimin
* Date: 2008-2-22 9:33:30
* /
StageBean {
String code;
String name;
String type;
String getCode() {
code;
}
setCode(String code) {
.code = code;
}
String getName() {
name;
}
setName(String name) {
.name = name;
}
String getType() {
type;
}
setType(String type) {
.type = type;
}
}
org.apache.commons.logging.Log;
org.apache.commons.logging.LogFactory;
com.topsoft.icisrpt.common.beans.StageBean;
com.topsoft.icisrpt.common.util.DBUtil;
java.util.Map;
java.util.HashMap;
java.sql.Connection;
java.sql.Statement;
java.sql.ResultSet;
java.sql.SQLException;
* Specific code utility classes
* File: StageCode.java
* User: leizhimin
* Date: 2008-2-22 9:35:43
* /
StageCode {
Log log = LogFactory.getLog(StageCode.
);
Map<String, StageBean> codeMap =
HashMap<String, StageBean>();
isLoad =
;
String sql =
+
;
{
reLoad();
}
* Private constructor, disallows object creation
* /
StageCode() {
}
* Reload period-specific code
*
* @return boolean
* /
reLoad() {
flag =
;
Connection conn = DBUtil.makeConnection();
Statement stmt =
;
{
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet rs = stmt.executeQuery(sql);
(rs.next()) {
StageBean stageBean =
StageBean();
stageBean.setCode(rs.getString(
));
stageBean.setName(rs.getString(
));
stageBean.setType(rs.getString(
));
codeMap.put(stageBean.getCode(), stageBean);
}
isLoad =
;
flag =
;
}
(SQLException e) {
log.error(
);
e.printStackTrace();
}
{
(stmt ! =
)
{
stmt.close();
}
(SQLException e) {
log.error(
);
e.printStackTrace();
}
(conn ! =
)
{
conn.close();
}
(SQLException e) {
log.error(
);
e.printStackTrace();
}
}
log.info(
+ (flag ?
:
));
flag;
}
* Get the period code table
*
* @return Map<String, StageBean>
* /
Map<String, StageBean> getCodeMap() {
codeMap;
}
* Determine whether the period code table has been loaded
*
* @return boolean
* /
isLoad() {
isLoad;
}
* SQL to obtain the query period code table
*
* @return String
* /
String getSql() {
sql;
}
}
java.util.Map;
java.util.Collection;
java.util.Iterator;
* Mock client test classes
* File: TestStageCode.java
* User: leizhimin
* Date: 2008-2-22 9:40:29
* /
TestStageCode {
main(String args[]) {
Map<String, StageBean> codeMap = StageCode.getCodeMap();
Collection<StageBean> col = codeMap.values();
System.out.println(
+ StageCode.getSql());
System.out.println(
);
System.out.println(
);
(Iterator it = col.iterator(); it.hasNext();) {
StageBean bean = (StageBean) it.next();
System.out.println(bean.getCode() +
+ bean.getName() +
+ bean.getType());
}
}
}
SELECT DM,MC,LX FROM RPT.DM_BBQB
——————
The following is the content of the code table:
D — Winter Quarterly Report — J
01 — 01 Monthly report — Y
July — July Monthly report — Y
C — Spring quarterly Report — J
02 — February Monthly report — Y
B — Annual report for the second half of the year — B
Q — Autumn quarterly report — J
June — June Monthly report — Y
03 — 03 Monthly report — Y
October — October monthly report — Y
04 — 04 Monthly report — Y
05 — 05 Monthly report — Y
A — First half annual report — B
11 — November monthly report — Y
September — September Monthly report — Y
N — Annual report — N
December — December monthly report — Y
08 — Monthly report of 08 — Y
X — Summer quarterly report — J
Process finished with exit code 0