Jump to content
Sign in to follow this  
newcoder1010

String comparison in If statement

Recommended Posts

Hi,

"select *  from seleniumuser where firstname = 'John'";

Returns 

fitstname | email

John John@aa.jj

Works just fine.

I like to have an IF statement to validate the first name. 

...
//Query to Execute		
String query = "select *  from seleniumuser where firstname = 'John'";	

//Create Statement Object		
Statement stmt = con1.createStatement();					

// Execute the SQL Query. Store results in ResultSet		
ResultSet rs= stmt.executeQuery(query);	

// While Loop to iterate through all data and print results		
while (rs.next()){
 		String myName = rs.getString("firstname");								        
        System. out.println(myName);		
                    
        if (myName == "John") {
           	System.out.println("Matched");
         } else {
           	System.out.println("Not matched");
         }
...
}		
		
	

Result I am getting:

John

Not matched.

It should return "Matched" instead. Please advise!

Share this post


Link to post
Share on other sites

Try myName.equals("John"). The strings may be pointing to different spaces in memory.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...