把你的数据库连接类写为static不就可以实现你的要求了吗?

解决方案 »

  1.   

    同意drugon(personal)
    封装一个连接数据库类,内建一个static对象,这样就只有一个类对象了如果是jsp就实现HttpSessionBindingListener接口,这样就把这个类与一个session绑定了
      

  2.   

    public class ConnectionFactory{
        private static DataSource dsCache = null;    aMethodForGetConnectio(){
            if(dsCache == null){
                synchronized(this){
                    if(dsCache==null){
                        Context ct = new InitialContext();
                        dsCache = (DataSource) ct.lookup(source);
                    }
                }
            }
            Connection conn = dsCache.getConnection();
        }
    }