Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem about if else usage in java with mysql.

My code is :

if (!result29.next() ) 
{
    System.out.println("No data");
} 
else {
    do {
        if(name1.equals(result29.getString("name2")))
        {
            //screen one  
            namedelete as = new namedelete();
            as.setVisible(true);
        }
        else
        {
            //screen two
            nameadd aek = new nameadd();
            aek.setVisible(true);
        }  
     } while (result29.next());
}

If I try to shortly tell this code, if name2 (a mysql colum result) equal to name1 (an entry value) opening the screen one, if not equal it is opening the screen two.

Problem

The problem is, if they are equal, screen one and screen two both opening. But, if not equal it is working normal, just screen two opening.

Can you help me, how can I fix that problem ?

share|improve this question
    
You are running a while loop. you may get both the if and else statement executed for all set of records. why don't you break out of loop when if condition fulfills? jut put break in if and else and you won't be getting both the results. –  user1502952 Aug 18 '13 at 20:51
    
@user1502952 Hmm ok, I will try, thanks. –  Harold Wren Aug 18 '13 at 22:06
    
@user1502952 it is working correctly now :) thank you –  Harold Wren Aug 18 '13 at 22:12

1 Answer 1

The most likely cause is your result set contains multiple rows, some that equal, some that don't.

I think if you debugged your program, you would find that screen two is displayed many times.

share|improve this answer
    
How can I fix that ? –  Harold Wren Aug 18 '13 at 22:05
    
Show us the SQL you are using. For your logic to work, your rowset must have at most 1 row. I can help with the query if you edit your question to include the query. Let md know Shen you've done that. –  Bohemian Aug 18 '13 at 23:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.