?
Solved

Minimaz Code

Posted on 2006-03-24
11
Medium Priority
?
225 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:princehyderabad
  • 6
  • 4
11 Comments
 
LVL 11

Expert Comment

by:fargo
ID: 16284865
<%
...
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
 
LVL 11

Expert Comment

by:fargo
ID: 16284875
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
 
LVL 11

Expert Comment

by:fargo
ID: 16284921
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:princehyderabad
ID: 16285290
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
 

Author Comment

by:princehyderabad
ID: 16285691
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
 
LVL 10

Accepted Solution

by:
radarsh earned 600 total points
ID: 16287051
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
 
LVL 11

Assisted Solution

by:fargo
fargo earned 400 total points
ID: 16289741
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
 
LVL 11

Expert Comment

by:fargo
ID: 16293301
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
 

Author Comment

by:princehyderabad
ID: 16325599
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
 
LVL 11

Expert Comment

by:fargo
ID: 16325741
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
 

Author Comment

by:princehyderabad
ID: 16325796
Agreed thanks :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question