首先说,我认为我们的系统结构设计的有问题。MVC的jsp+java框架。问题是这样的,数据库里有一个master表,存了一些基本信息(code和文字),现在是每次用code查相应的文字是都要从数据库里读,很烦。有没有什么方法让其在用户登陆后,只取一遍,就不用再去读数据库了。谢了,各位高手。
解决方案 »
- 基本数据类型的 == 比较
- 在 Java 中创建一个类会带来程序运行的负担?
- 为什么抽象类里的方法必须是静态的才可以使用
- 不显示删除回复显示所有回复显示星级回复显示得分回复 求计算机操作系统(修订版)(汤子瀛)的电子书
- 怎么java nio调用read时仍然是阻塞的呢?
- 求一正则表达式的写法,20分,答完既结
- 请问能够不用通道进行非阻塞IO编程吗?
- 怎样去掉字符串里ip地址!
- 我写了个五线程查找的程序,想优化但不知道怎么办(代码都有注释)
- javamail检查邮件附件的问题
- 新人在吐血,高手来看看啊 xiexie
- (请问,请帮忙)我用进栈的思想写了个删除字符串里重复的字符,但是有问题(附代码),想了好久也不知道错在哪?
还可以用cookie实现,具体的方法去网上找吧
客户后来重新链接时,这个用户ID会自动返回,用户无需每次都输入这些数据.
可用HttpSession session=request.getSession();
将登陆后查询出来的信息放到session里,用的时候再取出来
把代码贴出来大家看看。因为对于类的静态变量在第一次调用构造函数的时候,内存已经给分配空间了,所以再次new实例的时候,java内部只是再次把指针指向他,所以MasterController.java里我对静态变量categoryList是否为空做了判断。
这样,如果是第二次new实例就直接返回了。不过,不推荐大家这么用,不得已而为之。----------------------------------------------------调用:
MasterController dbmaster = new MasterController();
dbmaster.loadMaster(category);
String [] pref = dbmaster.getItemName();
String [] code = dbmaster.getItemCode();
-----------------------------------------------------------MasterController.javapublic class MasterController {
class CodeInfo {
public String code;
public String name;
}
class CategoryBloc {
public String categoryCode;
public int beginIndex;
public int endIndex;
}
private static ArrayList<CategoryBloc> categoryList = null;
private static ArrayList<CodeInfo> masterList = null;
private String [] itemCodeArray;
private String [] itemNameArray;
private Hashtable itemMasterHash;
public MasterController() {
if (categoryList != null) {
return;
}
categoryList = new ArrayList<CategoryBloc>();
masterList = new ArrayList<CodeInfo>();
DB db = null;
Connection con = null;
PreparedStatement ps = null;
try {
db = new DB();
con = db.getConnection();
ps=null;
ps = db.prepareStatement("SELECT * FROM TBL");
ResultSet rset = ps.executeQuery();
CategoryBloc categoryInfo = new CategoryBloc();
String categoryOld = "";
String categoryNew = null;
Integer index = 0;
Integer beginIndex = 0;
while(rset.next()){
categoryNew = rset.getString("CATEGORY");
if (!categoryOld.equals(categoryNew)) {
if (index > 0) {
categoryInfo = new CategoryBloc();
categoryInfo.categoryCode = categoryOld;
categoryInfo.beginIndex = beginIndex;
categoryInfo.endIndex = index - 1;
categoryList.add(categoryInfo);
beginIndex = index;
} if (categoryNew == null) {
categoryOld = "";
} else {
categoryOld = categoryNew;
}
}
CodeInfo cinfo = new CodeInfo();
cinfo.code = rset.getString("CODE");
cinfo.name = rset.getString("NAME");
masterList.add(cinfo);
index++;
}
if (!StringUtil.isNullStr(categoryOld)) {
categoryInfo = new CategoryBloc();
categoryInfo.categoryCode = categoryOld;
categoryInfo.beginIndex = beginIndex;
categoryInfo.endIndex = index - 1;
categoryList.add(categoryInfo);
}
}
catch (Exception ex){
DrsDBLogger.error(ex.getMessage());
}
finally {
db.close();
}
}
public boolean loadMaster(String category){
if (categoryList == null || category == null) {
return false;
}
for (int i = 0; i < categoryList.size(); i++) {
if (category.equals(categoryList.get(i).categoryCode)) {
CategoryBloc categoryInfo = categoryList.get(i);
int len = categoryInfo.endIndex - categoryInfo.beginIndex + 1;
itemCodeArray = new String[len];
itemNameArray = new String[len];
itemMasterHash = new Hashtable();
int num = 0;
for (int index = categoryInfo.beginIndex; index <= categoryInfo.endIndex; index++, num++) {
itemCodeArray[num] = masterList.get(index).code;
itemNameArray[num] = masterList.get(index).name;
// HashTable作成
itemMasterHash.put(masterList.get(i).name, masterList.get(i).code);
}
break;
}
}
return true;
}
public boolean loadMaster(String... category){ String categoryStr = null;
int numberOfCategory = 0;
if (categoryList == null || category == null) {
return false;
}
numberOfCategory = category.length;
for (int i = 0; i < numberOfCategory; i++) {
categoryStr = category[i];
for (int j = 0; j < categoryList.size(); j++) {
if (categoryStr.equals(categoryList.get(j).categoryCode)) {
CategoryBloc categoryInfo = categoryList.get(j);
int len = categoryInfo.endIndex - categoryInfo.beginIndex + 1;
itemMasterHash = new Hashtable();
int num = 0;
for (int index = categoryInfo.beginIndex;
index <= categoryInfo.endIndex;
index++, num++) { // HashTable作成
itemMasterHash.put(masterList.get(i).name, masterList.get(i).code);
}
break;
}
}
} return true;
} public String[] getItemCode(){
return itemCodeArray;
} public String [] getItemName(){
return itemNameArray;
}
/**
* @return itemMasterHash を戻します。
*/
public Hashtable getItemMasterHash() {
return itemMasterHash;
}
}