Jump to content

String comparison in If statement


Recommended Posts


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


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") {
         } else {
           	System.out.println("Not matched");

Result I am getting:


Not matched.

It should return "Matched" instead. Please advise!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...