Results 1 to 7 of 7
  1. #1
    kishan.java is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default problem with if statement in while loop of result set

    Hi All,

    I am new to java.

    I am faciing the below problem in java .


    while(rs9.next())

    if (rs9.getString(5).equals("Grade")){
    System.out.println(":kishan test data in Grade id......+++ " +rs8.getString(6));
    } else if (rs9.getString(5).equals("Business_Title")){ System.out.println(":kishan test data in Business_Title is......+++ " +rs8.getString(6));
    } else if (rs9.getString(5).equals("Anticipated_StartDat")){ System.out.println(":kishan test data in Anticipated_StartDat is......+++ " +rs8.getString(6));
    et.setCostCenter(rs1.getString(6));
    }



    }
    My pronblem is if i remove all if and else if and put only system.out.println.. i am able to see all records inside while.

    if i put one if condition, i am able to get the data.

    if i put more than on if condition then i am getting exhausted result set...

    and also not able to see result of third else condition.....



    please let me know ..how to solve my issue..

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There are things in that code that are either typos when you posted or actual errors in the code.

    Post again being careful to copy and post the actual code you are using.

    And a couple of formatting changes will make the code a lot clearer:

    (1) When you post here use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the indentation will be preserved on the web page.

    (2) Avoid long lines and especially don't put any code past an opening brace ({) - rather code within an if (or any other) block should be indented and placed on subsequent lines.

    if i put more than on if condition then i am getting exhausted result set...

    Copy and post the exact entire runtime error message you get when you run the code. If it refers to some line of your code (and it will), indicate somehow which line it is talking about.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    put the If statement in, if the data is not being printed when it should be it means the condition you put in the If statement is not coming true, so thats where your error is (in the condition)

  4. #4
    kishan.java is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    My problem is as below:

    Java Code:
     while(rs9.next())
    
    {  
    
      if (rs9.getString(5).equals("SAP_Job_Title"))
    {
    		    			    		      System.out.println(":kishan test data in SAP_Job_Title is......+++  " +rs8getString(6));
      }
       if (rs9.getString(5).equals("BG"))
    {
     System.out.println(":kishan test data in Bussiness Group is......+++  " +rs8.getString(6));
      }
    if (rs9.getString(5).equals("BU"))
    {
    System.out.println(":kishan test data in Bussiness Unit is......+++  " +rs8.getString(6));
      }
    if (rs9.getString(5).equals("Site_Code"))
    {
    System.out.println(":kishan test data in Site code is......+++  " +rs8.getString(6));
      }
     if (rs9.getString(5).equals("Grade"))
    {
    System.out.println(":kishan test data in  Grade id......+++  " +rs8.getString(6));
     } 
    if (rs9.getString(5).equals("Business_Title"))
    {
    System.out.println(":kishan test data in Business_Title is......+++  " +rs8.getString(6));
     }
     if (rs9.getString(5).equals("Anticipated_StartDat"))
    {
     System.out.println(":kishan test data in Anticipated_StartDat is......+++  " +rs8.getString(6));
      }
    
    
    }

    in my while loop , when i put only one if condition i am getting the data for that condition.

    if i remove all If condtions and put system.println.... i am able to see all records in console.

    if i put more than two " if "conditions as in above code, i am not able to see all the records in console. I am getting only one record and Exhausted result set error.

    please let me why the problem is???
    Last edited by Fubarable; 04-09-2011 at 04:45 PM. Reason: code tags added

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator Edit: Code tags added to the post above.

    To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

  6. #6
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    I am not sure if this will help in your code since you didn't state clearly what it is supposed to do, but
    1) I think instead of
    while(rs9.next())
    you mean if rs9 has next element or input, so it is to be
    while(rs9.hasNext())
    2) for it not to give "Exhausted result" I was taught that it is better to declare anything you use many times, in you case you overused
    rs9.getString(5)
    so why don't you initiate it to a String so that you compare this String instead of reading the rs9 every time... the same is for
    rs8.getString(6)
    3) for you to save time and effort and for your programs efficiency it is better to use if else statements so that if a condition is satisfied it doesn't continue looping

    4) you can return a String if every if statement and then print the whole sentence,
    this will do you so good especially to help you and other to understand your code clearly

  7. #7
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    This is a clearer demonstration for you if you didn't really understand what i meant :
    Java Code:
            String str5 = rs9.getString(5);
            String differentWord = "";
            
            while(rs9.hasNext()){
                if (str5.equals("SAP_Job_Title"))
                    differentWord = "SAP_Job_Title";
                else if (str5.equals("BU"))
                    differentWord = "Bussiness Group";
                
                    // you continue all your conditions as an "if-else" statments
                    // after all your conditions you print the following
                System.out.println(":kishan test data in" + differentWord + " is......+++ " + rs9.getString(6));
            }

    I am sure that in this way your code is more readable and clearer for anyone (besides yourself) to understand, try this and then state then errors you encounter please

Similar Threads

  1. Replies: 3
    Last Post: 02-25-2011, 05:34 PM
  2. Help with loop statement
    By arvind1508 in forum New To Java
    Replies: 2
    Last Post: 02-23-2011, 05:39 PM
  3. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 04:42 AM
  4. Least To Greates[ if statement and for loop]
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 07:34 PM
  5. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 02:41 AM

Posting Permissions

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