写个单例模式吧.一个学校-->N 个年级-->N个班级--> N个学生.在INIT方法里面加载全部数据.以后的所有查询都不要再读数据库.每次增删改一要更改数据库,二要更新这个单例类(这样做查询到的数据才能是和数据同步的).别说很简单啊~~~

解决方案 »

  1.   

    singleton模式主要是用在数据库连接上吧,你说的那个应该是CacheRowSet吧.把数据库的信息加载到内存中,然后进行查询吧.//连接类 TestConn.java
    public class TestConn {
    private DataSource ds = null;
    private static DbConnection instance = null; private DbConnection() {

    } public Connection getConnection() {
    Connection conn = null;
    try {
    if(ds==null){
      Context ctx = new InitialContext(); //初始化上下文
      ds = (DataSource) ctx.lookup("XXXXXXX");
    }
    conn = ds.getConnection();
    } catch (NamingException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return conn;
    } public static DbConnection getInstance() {
    if (instance == null) {
    instance = new DbConnection();
    }
    return instance;
    }
    }
    //管理类TestManager.java 把数据库的信息加载在CacheRowSet里
    public class TestManager
    {
    public boolean updateSQL(String sql) {
    boolean bln = false;
    Connection conn = DbConnection.getInstance().getConnection();
    try {
    Statement stmt = conn.createStatement();
    int rows = stmt.executeUpdate(sql);
    if (rows > 0)
    bln = true;
    stmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    try {
    if (conn != null)
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } return bln;
    } public CachedRowSet querySQL(String sql){
    CachedRowSet crs = null;
    Connection conn= DbConnection.getInstance().getConnection();
    try {
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    crs = new CachedRowSet();
    crs.populate(rs);
    rs.close();
    stmt.close();

    } catch (SQLException e) {
    e.printStackTrace();
    }finally{
    try {
    if (conn != null)
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    return crs;
    }
    }
      

  2.   

    恩.谢谢.standsky.
    我说的的确就是CACHE.具体说应该是业务类的CACHE 实现.所以不是数据库连接问题.应该是业务的问题.我这里的学校年级班级学生都有自己的DAO类,只是想有个CacheSchool类.能一次全加载.
    以后的的查询都从CacheSchool类里直接拿,不是在调用DAO程序.
    我想来想去,有点繁的地方就是每次增删改的时候同步这个CacheSchool类.
    (比如新加了个学生,当然可以重新全部取得该班级的学生以实现同步,但是我觉得就已经不是个最优化的处理了.)想有个优化的想法.