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?
 
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
0
 
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
0
 
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
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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
0
 
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 :)
0
 
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>");
               }
0
 
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
0
 
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
0
 
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.,
0
 
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
0
 
princehyderabadAuthor Commented:
Agreed thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.