Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: Caching Database Queries


Message #1 by "Lior Shliechkorn" <lior@f...> on Mon, 3 Feb 2003 17:39:21
Hi

Just put the result of a query in the session and will be available 
throughout the session, the query will not be executed everytime

Vector vecEmpNames = (Vector)application.getAttribute("empNames");
  if (vecEmpNames == null){
    try{
      vecEmpNames = new Vector();

      Connection conn = DriverManager.getConnection("<jdbc_driver");
      String select = "select distinct emp_name from Emp";
      Statement stmt = conn.createStatement();
      ResultSet resultSet = stmt.executeQuery(select);

      while(resultSet.next()){
        vecEmpNames .addElement(resultSet.getString("emp_name"));
      }
      resultSet.close();
      stmt.close();
      conn.close();
    
      application.setAttribute("empNames", vecEmpNames );
    }
    catch(Exception e){
      System.err.println(e);
    }

Ash






> Hello,

> I've tried to find out information on how to go about doing this, but I 
c> an't find a good example or discussion to direct me in the right 
d> irection.

> I want to cache a query so that I can generate a report, as well as have 
a 
u> tility class generate a .csv file so the report can be saved for later 
u> se. Now I want to be able to have both of these run one after the 
other, 
b> ut I don't want to run the same quries twice since it's a big report.

> Now, should I store the ResultSets in a bean? How do I cache the 
queries? 
I> f it's alright, I would love to see an example.

> Thank you very much,
L> ior 

  Return to Index