首先说,我认为我们的系统结构设计的有问题。MVC的jsp+java框架。问题是这样的,数据库里有一个master表,存了一些基本信息(code和文字),现在是每次用code查相应的文字是都要从数据库里读,很烦。有没有什么方法让其在用户登陆后,只取一遍,就不用再去读数据库了。谢了,各位高手。

解决方案 »

  1.   

    你可以将登陆后查询出来的信息放到session里,用的时候再取出来,这个session的作用域是整个请求(request)!!!
    还可以用cookie实现,具体的方法去网上找吧
      

  2.   

    用户基本信息一般存在session范围内
      

  3.   

    对哪些安全性要求较低的站点,可用cookie.用户注册后,就会收到包含一个唯一用户ID的cookie.
    客户后来重新链接时,这个用户ID会自动返回,用户无需每次都输入这些数据.
    可用HttpSession session=request.getSession();
    将登陆后查询出来的信息放到session里,用的时候再取出来
      

  4.   

    每一个用户登录以后在application-ServletContext中创建一个该用户的工作区(其实就是一个对象),储存该用户的一些关键信息,然后如果在使用这些信息的时候就从ServletContext中读取,该用户注销后,删除其工作区.
      

  5.   

    多谢两位,不过,如果不考虑session呢。这样session会比较重
      

  6.   

    不考虑session,那就把信息存储在文件里,硬盘存取
      

  7.   

    结贴了。谢谢各位。最后是放在内存理了。虽然我觉得不是很好,但是这里是改别人的代码,限制太多了。
    把代码贴出来大家看看。因为对于类的静态变量在第一次调用构造函数的时候,内存已经给分配空间了,所以再次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;
    }
    }