Solved

Problem while iterate in Two loops

Posted on 2009-07-06
9
194 Views
Last Modified: 2012-05-07
hi all,
         in reference to my earlier post,  your comment "
         Additionally u can pass all ids as a string to the above qery and take the output in a Map and then iterate in a single loop. "
        Can you please let me know how can i do that ?

Awaiting your expert comment...
0
Comment
Question by:NASEEMA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:Murali Murugesan
ID: 24791991
Something like this,

your value in variable tprID and seq_nr should be like this,

tprID : 'MN4676','4567TR','TU36363'
seq_nr: '2','4','6'  -- (i guess values should be unique since sequence number)

public Map getLogCounter(String tprID,String seq_nr) {
                                    Map counterMap = new HashMap();
                                     String sql="SELECT opn_seq_nr,COUNT(*)"
                                                       +" FROM   log"
                                                       +" WHERE  opn_id IN ("+tprID
                                                       +") AND opn_flag ='TPR'"
                                                       +" AND opn_seq_nr IN ("+seq_nr
                                                         +") AND upd_flag  IS NULL GROUP BY opn_seq_nr ";      
                               //PreparedStatement - implement this
                                         //resultset - implement this

                                   while(rs.next()){
                                          counterMap .put(rs.getString(1),rs.getInt(2));
                                    }      
                              return counterMap ;                              
                              }


Map values after iteration should look like below,

2  20
4  60
6  90


Now get the map values and get the count for each sequence number and iterate the second loop.

-Murali*

0
 

Author Comment

by:NASEEMA
ID: 24796926
Hi,
  Sorry for ver late reply.
   seq_nr is not unique. and i feel i couldnt be able to implement code.is  There any other possibilty in achieve my task
 
0
 

Author Comment

by:NASEEMA
ID: 24800760
HI All,
        Any other possibility in achieve my task..


0
Technology Partners: 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!

 
LVL 13

Expert Comment

by:Murali Murugesan
ID: 24800913
is your tprID is unique? If not then replace the select as follows,

SELECT opn_seq_nr || '~' ||opn_id,COUNT(*)

Now a combination of sequence and tprID  will be unique. Now u can implement the above code.

Even if this combination is not unique i would suggest u to use a bean class object (i hope u already have one) as the value and key for the map as some unique running number.

I feel this code would reduce you most of the delay and also we need to see if we can refactor the next loop as well once u implement this.

-Murali*
0
 
LVL 13

Expert Comment

by:Murali Murugesan
ID: 24810777
did u try to implement this function ? or still facing problem in implementing it?
0
 

Author Comment

by:NASEEMA
ID: 24810960
Hi,
   i  couldnt be able to implemnt your last input.
  i appreciate if you can write the whole code so that i will copy and paste the code .
  sorry that i am asking u to write the code.

0
 
LVL 13

Accepted Solution

by:
Murali Murugesan earned 500 total points
ID: 24811012
I think its a very simple implementation. Here you go,


                   public Map getLogCounter(String tprID,String seq_nr) {
                                    Map counterMap = new HashMap();
                                     String sql="SELECT opn_seq_nr || '~' ||opn_id,COUNT(*)"
                                                       +" FROM   log"
                                                       +" WHERE  opn_id IN ("+tprID
                                                       +") AND opn_flag ='TPR'"
                                                       +" AND opn_seq_nr IN ("+seq_nr
                                                         +") AND upd_flag  IS NULL GROUP BY opn_seq_nr ";      
                                       Connection conn = ConnectionManager.getConnection();
                                       PreparedStatement pstmt = null;
                                       ResultSet rs= null;

                                      try{
                                               pstmt = conn.prepareStatement(sql);
                                               rs = pstmt.executeQuery();
                                                while(rs.next()){
                                                        counterMap .put(rs.getString(1),rs.getInt(2));
                                                 }      
                                         }catch(Exception e){}
                                         finally{
                               try {
                        if (!conn.isClosed()) {
                              ConnectionManager.releaseConnection(conn);
                        }
                                 
                         } catch (SQLException e1) {
                        e1.printStackTrace();
                         }
                          }                                                                            
                              return counterMap ;                              
                              }

Now u will get a map with values as ,

2~MN4676   20
4~4567TR    60
6~TU36363  90


Now iterate the map and get the count and use it in the next loop, thereby first loop can go away.
Hope u will try and do the remaining part.

-Murali*

0
 

Author Comment

by:NASEEMA
ID: 24811069
HI,
  Thanks a lot for the help.i will accept ur comment .I appreciate if you can let me know the remaining code too....

sorry again to request to write code for me....:
0
 
LVL 13

Expert Comment

by:Murali Murugesan
ID: 24811271
Sorry Naseema, this time you need to give your best try and make up the code on your own.
You do the code and fix the bugs and if you are caught in somewhere we might guide you.

Out of the two for loops i have showed you how to minimize one loop. You get the count from this and iterate the next loop.

Thanx
-Murali*
0

Featured Post

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Can't access image with tomcat/html 6 107
maven project jars 7 58
mysql jsp example issue 32 75
spring maven example issues 3 44
While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
Original post  on Monitis Blog. Web performance monitoring is broken into two camps: passive and active. Passive monitoring is defined as looking at real-world historical performance by monitoring actual log-ins, site hits, clicks, requests for…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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