写个单例模式吧 写个单例模式吧.一个学校-->N 个年级-->N个班级--> N个学生.在INIT方法里面加载全部数据.以后的所有查询都不要再读数据库.每次增删改一要更改数据库,二要更新这个单例类(这样做查询到的数据才能是和数据同步的).别说很简单啊~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 singleton模式主要是用在数据库连接上吧,你说的那个应该是CacheRowSet吧.把数据库的信息加载到内存中,然后进行查询吧.//连接类 TestConn.javapublic 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; }} 恩.谢谢.standsky.我说的的确就是CACHE.具体说应该是业务类的CACHE 实现.所以不是数据库连接问题.应该是业务的问题.我这里的学校年级班级学生都有自己的DAO类,只是想有个CacheSchool类.能一次全加载.以后的的查询都从CacheSchool类里直接拿,不是在调用DAO程序.我想来想去,有点繁的地方就是每次增删改的时候同步这个CacheSchool类.(比如新加了个学生,当然可以重新全部取得该班级的学生以实现同步,但是我觉得就已经不是个最优化的处理了.)想有个优化的想法. 求余 这是什么问题? API文档 String 类的对象在程序结束后,jvm能否将其回收。 关于小应用程序的问题? 谁能解决?10本电子书籍作为报酬 ... 周一要去面试了。 关于tomcat 关系Float的除法。好像不妥。 高分求购做饼图、线形图的源吗? 兄弟们,帮我注释下这段代码,关于TextArea 参数传递问题。。
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;
}
}
我说的的确就是CACHE.具体说应该是业务类的CACHE 实现.所以不是数据库连接问题.应该是业务的问题.我这里的学校年级班级学生都有自己的DAO类,只是想有个CacheSchool类.能一次全加载.
以后的的查询都从CacheSchool类里直接拿,不是在调用DAO程序.
我想来想去,有点繁的地方就是每次增删改的时候同步这个CacheSchool类.
(比如新加了个学生,当然可以重新全部取得该班级的学生以实现同步,但是我觉得就已经不是个最优化的处理了.)想有个优化的想法.