Minimaz Code

hi experts,

I have this code: working fine just need to minimaz as its calling same resultset at two places:
<%
...
AL = //DB Query-1;  
        rs2=stmt3.executeQuery(AL);               
              while (rs2.next())                   
        {                   
        %>
        <select name="approval_level" >
            <option><%=rs2.getString("APPROVAL_NAME");%></option>
            <%
        }                   
        rs2.close();        
        %>      
        </select>
...
...
// other DB code etc.,
...
..
//NOW AGAIN i WANNA DISPLAY SAME <dropdown> as above code: How to manage without copying same code here ???
...


Thx,
PH
princehyderabadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fargoCommented:
<%
...
AL = //DB Query-1;  
java.util.ArrayList approvals = new java.util.ArrayList();
        rs2=stmt3.executeQuery(AL);              
            while (rs2.next())                
        {                
         approvals.add(rs2.getString("APPROVAL_NAME"));
        }                  
        rs2.close();        
        %>      
       
// iterate over the apporvals array list and populate the select options
        <select name="approval_level" >
          <option><%=approvals.get(i);%></option>
        </select>
...
...
// other DB code etc.,
...
..
//NOW AGAIN i WANNA DISPLAY SAME <dropdown> as above code: How to manage without copying same code here ???
// iterate over the apporvals array list and populate the select options
        <select name="approval_level" >
          <option><%=approvals.get(i);%></option>
        </select>
...

fargo
fargoCommented:
Hi,

don't just copy and paste the above code. Like in place where i said, iterate over the arraylist...u need to do the iteration.

Hope it helps.

fargo
fargoCommented:
Hi princehyderabad,

I saw that you are making too much use of resultset handling in your jsps. As per your last posts...i do see that you use struts too. If you are using struts, then why don't u make use of action classes to make the communication with model and keep your jsps clean with the help of tags.

In case you need information about how to handled Select list with struts...see my post there
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21767785.html

regards,
fargo
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

princehyderabadAuthor Commented:
Actually I have 2 values Approval_Name and Approval_Level
One displays while other internal assign values
<option value="Approval_Level">Approval_Name</option>

having Problem in doing Iterate ... can U put code please :)
princehyderabadAuthor Commented:
Now how to get ALevels ?? in <option value="???">


java.util.ArrayList ALevels = new java.util.ArrayList();
java.util.ArrayList ANames = new java.util.ArrayList();
...
....
for (Iterator it = ANames.iterator(); it.hasNext (); )
              {
                out.print("<option>"+it.next()+"</option>");
               }
radarshCommented:
both your ArrayLists will be of the same size. So, you can do

java.util.ArrayList ALevels = new java.util.ArrayList();
java.util.ArrayList ANames = new java.util.ArrayList();
........
........

for(int i = 0; i < ALevels.size(); i++)
{
    out.print("<option value="+ALevels.get(i)+">"+ANames.get(i)"+</option");
}

Note: For ArrayList, using an indexed for loop is much faster than using
an Iterator.

________
radarsh

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fargoCommented:
Hi,

i would suggest you the following..
1) Make a simple bean with approval name and level
2) populate it with resultset
3) Use them in codes whereever in the jsp

1) bean like

public class Approvals {

      private String approvalName;
      private String approvalLevel;
      
      public String getApprovalLevel() {
            return approvalLevel;
      }
      public void setApprovalLevel(String approvalLevel) {
            this.approvalLevel = approvalLevel;
      }
      public String getApprovalName() {
            return approvalName;
      }
      public void setApprovalName(String approvalName) {
            this.approvalName = approvalName;
      }
}

2) JSP result population

AL = //DB Query-1;  
java.util.ArrayList list = new java.util.ArrayList();
Approvals bean = null;
        rs2=stmt3.executeQuery(AL);              
            while (rs2.next())                
        {            
           bean = new Approvals();
           bean.setApprovalName(rs2.getString("APPROVAL_NAME"));
           bean.setApprovalLevel(rs2.getString("APPROVAL_LEVEL"));
         approvals.add(bean);
        }                  
        rs2.close();    

3) Now instead of keeping two array list for level and name. Use one and iterate over them to populate the select list

hope it helps.
fargo
fargoCommented:
for iterating over the list of beans and printing out the options you can do the following

for(int i = 0; i < list.size(); i++)
{
    Approvals appBean = (Approvals) list.get(i);
    out.print("<option value="+appBean.getApprovalLevel()+">"+appBean.getApprovalName()+"</option");
}

regards,
fargo
princehyderabadAuthor Commented:
with "radarsh" code i was able to do my job. Is there a reason to go with bean class. I may take few extra steps etc.,
fargoCommented:
Well, surely you can solve it the way radarsh explained.

The only reason i demo the bean way is to make code clear and more and more use of bean to move close to MVC style code.

fargo
princehyderabadAuthor Commented:
Agreed thanks :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.