We help IT Professionals succeed at work.

help with JavaBean solution for  - Reduce Database calls - need array?

wykzimme
wykzimme asked
on
228 Views
Last Modified: 2010-04-01
I would like to see if I can reduce the number of database calls.  Currently I have the following code in my JSP which can run 2 to 99 times for a page.

I am not sure how to use a JavaBean to connect to the database and get all the records and display them on my web page.  
I want to create a Bean(s) and give it scope of application.

What needs to go where in the bean to get the database records to load?

FlightRequest.jsp
<code>
<jsp:useBean
  id="fltReqInfo"
  scope="session"
  class="us.wy.state.dot.aero.beans.RequestInfo" >
</jsp:useBean>
<jsp:setProperty name="fltReqInfo" property="*"/>
...
<b>Department or Agency:</b>
<br>
<select size="4" multiple="false" name="depName">
<%
  ResultSet rsetAgen = connection.executeQuery("SELECT AGENCY_NUMID,AGENCY_NAME FROM flightsch.state_agencies ORDER BY agency_name");
  while(rsetAgen.next())  
    {
      String ag_num = rsetAgen.getString("AGENCY_NUMID");
      String ag_name = rsetAgen.getString("agency_name");
   
%>
        <option <%= fltReqInfo.getDepName().equals(ag_num) ? "selected":"" %> value="<%= ag_num%>"><%= ag_name%></option>
<%
    }     //end of while loop
  rsetAgen.close();
%>
</select>
...
</code>
I am using a bean, fltReqInfo , to validate user data entry.

AgencyInfo.java
<code>
package us.wy.state.dot.aero.beans;

/**
 * This class contains information about the state agencies.
 * It contains the agency number and agency description
 * there are 57 records in the database (3 fields: agency_numid, agency_charid, & agency_name)
 *
 * @author x
 * @version 1.00, 12 Feb. 2004
 */
 
public class AgencyInfo
{
  private int agency_numid;
  private String agency_charid;
  private String agency_name;

  public AgencyInfo()
  {
    this.agency_numid = 0;
    this.agency_charid = "";
    this.agency_name = "";
  }

  public int getAgency_numid()
  {
    return this.agency_numid;
  }

  public void setAgency_numid(int newAgency_numid)
  {
    this.agency_numid = newAgency_numid;
  }

  public String getAgency_charid()
  {
    return this.agency_charid;
  }

  public void setAgency_charid(String newAgency_charid)
  {
    this.agency_charid = newAgency_charid;
  }

  public String getAgency_name()
  {
    return this.agency_name;
  }

  public void setAgency_name(String newAgency_name)
  {
    this.agency_name = newAgency_name;
  }
}
</code>
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Are you workin of a load balanced se of servers. In that case you may want to be careful on how you use your singleton
That is true. but in this particular use case, singleton is used for reduce duplicated object and process, so it doesn't has to be a true singleton.
BTW, singleton problem in a distributed environment usually can be resolved by using an entity ejb.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.