Results 1 to 12 of 12
  1. #1
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default difficulties with IF statement condition

    Hello All,

    I am having difficulties while preparing a jsp report. Here is how does it look:

    Name Item

    Kane 123
    Kane 333
    Kane 434
    Kim 444
    What I am trying to do is that group by Name

    Kane 123
    333
    434
    Kim 444
    If the next name = the previous name then hide the name, otherwise show the name.

    Here is my code:
    Java Code:
    while(rs.next()){ 
     
    %> 
            <tr> 
                <td width="238"> 
                    <%temp=rs.getString("TNAME");%>  
                    <%=temp%> </td> 
                <td><%=rs.getString("ID")%></td> 
                        <% 
                               if (rs.next().equals(temp){  
                temp=""; 
                } 
     
     
    } 
     
    rs.close();
    The above code is not showing what I want, so how do I change the code and do a group for each name.

    Thanks for your help

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Java Code:
    if (rs.next().equals(temp)
    You're comparing the return value of rs.next() with temp, not the value of the next NAME string. rs.next() returns a boolean (which will be wrapped into a Boolean)...so that if will always return false.

  3. #3
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default

    SO, what do you suggest?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Does rs have a method that returns a value you could compare to temp's value?

  5. #5
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default

    This is what I am trying to get.

    Suppose my jsp is:

    Java Code:
    while(rs.next()){ 
     
    %> 
            <tr> 
                <td width="238"> 
                    <%temp=rs.getString("TNAME");%>  
                    <%=temp%> </td> 
                <td><%=rs.getString("ID")%></td> 
                        <% 
        
    } 
     
    rs.close();
    So, what do I have to add if I want to show duplicated name as blank " "

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    OK, I will point out I am not a fan of having resultsets and db operations in a JSP. There's lots of stuff that can go wrong (like not closing connections, statements, result sets properly)...however, you're here now.

    You want something to hold the previousName, which will be declared outside the loop and initialised to blank. Then set the temp variable as you are now. If temp does not equal previousName then display temp and set previousName to temp.

  7. #7
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default

    Do you mean that I will store the result in a temp which is already there.
    Now where I have to compare is it after the rs.next() or at the end?
    How my compare will be, is it like:
    (rs.next().equals(temp) or another thing?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    You only want to display the name if the current name is different from the previous name, so the if goes where you are doing the display. Should be pretty obvious.

    You need two variables...one for the previous name and one for the current name (which is your temp variable).

    Try it out.

  9. #9
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default

    Now, my question is: Should the IF statement be at the end of the while loop or at the beginning.
    I have set a temp variable for the resultset and a temp at the beginning. But, the results are not coming

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Without seeing what you've written it's hard to say what you're doing wrong.

  11. #11
    murali.java is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    I hope below code helps you in solving your problem.....

    <%
    String temp="",tname="";
    ResultSet rs=st.executeQuery("your query");
    while(rs.next())
    {
    // store the value of TNAME from database in the variable tname
    tname=rs.getString("TNAME");
    // if previous name is not equal to next name then display the name ...
    if(!(temp.equals(tname)))
    {
    out.println("<br>"+tname);
    // store the previous value in the variable temp
    temp=tname;
    }// end of if
    %>
    <BR><%=rs.getString("ID")%>
    <% } %> // end of while loop

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

Similar Threads

  1. Difficulties with many-to-many association
    By berlindutza in forum Web Frameworks
    Replies: 0
    Last Post: 03-03-2010, 04:53 PM
  2. Difficulties using Hibernate + MySQL
    By berlindutza in forum Web Frameworks
    Replies: 3
    Last Post: 02-17-2010, 09:23 AM
  3. Difficulties randomizing images in an array
    By phb5004 in forum New To Java
    Replies: 2
    Last Post: 12-11-2009, 04:45 AM
  4. Difficulties logging onto Sun Developer's Network
    By jocassid in forum New To Java
    Replies: 0
    Last Post: 07-01-2008, 02:02 AM
  5. JOptionPane Display Difficulties
    By kewlgeye in forum New To Java
    Replies: 7
    Last Post: 05-09-2008, 08:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •