In Java application systems, often there will be a lot of code table to deal with, mainly in the processing needs through code name, access to the code of other information, if every time by querying the database to handle, is will consume large amounts of resources, too slow, because every time connection to connect to the database to the cost is high.


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


* /


public
class

StageBean {


private

String code;

/ / code


private

String name;

/ / name


private

String type;

/ / type





public

String getCode() {


return

code;


}





public
void

setCode(String code) {


this

.code = code;


}





public

String getName() {


return

name;


}





public
void

setName(String name) {


this

.name = name;


}





public

String getType() {


return

type;


}





public
void

setType(String type) {


this

.type = type;


}


}

import

org.apache.commons.logging.Log;


import

org.apache.commons.logging.LogFactory;


import

com.topsoft.icisrpt.common.beans.StageBean;


import

com.topsoft.icisrpt.common.util.DBUtil;





import

java.util.Map;


import

java.util.HashMap;


import

java.sql.Connection;


import

java.sql.Statement;


import

java.sql.ResultSet;


import

java.sql.SQLException;





/ * *


* Specific code utility classes


* File: StageCode.java


* User: leizhimin


* Date: 2008-2-22 9:35:43


* /


public
final
class

StageCode {


private
static
final

Log log = LogFactory.getLog(StageCode.

class

);


private
static

Map<String, StageBean> codeMap =

new

HashMap<String, StageBean>();


private
static
boolean

isLoad =

false

;


private
static
final

String sql =

“\n”

+


“SELECT DM,MC,LX FROM RPT.DM_BBQB”

;





static

{


reLoad();


}





/ * *


* Private constructor, disallows object creation


* /


private

StageCode() {


}





/ * *


* Reload period-specific code


*


* @return boolean


* /


public
static
boolean

reLoad() {


boolean

flag =

false

;


Connection conn = DBUtil.makeConnection();


Statement stmt =

null

;


try

{


stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);


ResultSet rs = stmt.executeQuery(sql);


while

(rs.next()) {


StageBean stageBean =

new

StageBean();


stageBean.setCode(rs.getString(

“DM”

));


stageBean.setName(rs.getString(

“MC”

));


stageBean.setType(rs.getString(

“LX”

));


codeMap.put(stageBean.getCode(), stageBean);


}


isLoad =

true

;


flag =

true

;


}

catch

(SQLException e) {


log.error(

“Loading date code table exception!”

);


e.printStackTrace();


}

finally

{


if

(stmt ! =

null

)

try

{


stmt.close();


}

catch

(SQLException e) {


log.error(

“Failed to close the database Statement object!

);


e.printStackTrace();


}


if

(conn ! =

null

)

try

{


conn.close();


}

catch

(SQLException e) {


log.error(

“Failed to close database connection!”

);


e.printStackTrace();


}


}


log.info(

“Loading period code table”

+ (flag ?

“Success!

:

“Failure!

));


return

flag;


}





/ * *


* Get the period code table


*


* @return Map<String, StageBean>


* /


public
static

Map<String, StageBean> getCodeMap() {


return

codeMap;


}





/ * *


* Determine whether the period code table has been loaded


*


* @return boolean


* /


public
static
boolean

isLoad() {


return

isLoad;


}





/ * *


* SQL to obtain the query period code table


*


* @return String


* /


public
static

String getSql() {


return

sql;


}


}

import

java.util.Map;


import

java.util.Collection;


import

java.util.Iterator;





/ * *


* Mock client test classes


* File: TestStageCode.java


* User: leizhimin


* Date: 2008-2-22 9:40:29


* /


public
class

TestStageCode {


public
static
void

main(String args[]) {


Map<String, StageBean> codeMap = StageCode.getCodeMap();


Collection<StageBean> col = codeMap.values();


System.out.println(

The SQL code for the query is:

+ StageCode.getSql());


System.out.println(

“– — — — — — — — — — — — — — — — — –”

);


System.out.println(

“Here is the code table to query the content:”

);


for

(Iterator it = col.iterator(); it.hasNext();) {


StageBean bean = (StageBean) it.next();


System.out.println(bean.getCode() +

“–”

+ bean.getName() +

“–”

+ bean.getType());


}


}


}

Running results:
The SQL code for the query is:


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

Welcome to leave a message.