String Value not working in android
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/3576413/why-is-my-string-to-string-comparison-failing">Why is my string to string comparison failing?</a>  </p>
</blockquote>



<p>I am unable to store assign a value to string variable , I have given my program below </p>

<pre><code> public class MahindraActivity extends Activity {
    EditText userid,pwd1;
    Button loginbut;
    Statement st;
    ResultSet rs;
    Connection con;
    String userrole1;
    //String userid1,pass1;
    String url="jdbc:microsoft:sqlserver://10.0.2.2:1433;DatabaseName=swingmail";
    String user="sa";
    String pwd="sa";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mahindra);
        userid=(EditText) findViewById(R.id.uname);
       //String userid1=userid.getText().toString();
        pwd1=(EditText) findViewById(R.id.password);
        //String pass1=pwd1.getText().toString();
        loginbut=(Button) findViewById(R.id.login);
        loginbut.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MahindraActivity.this, "Please Wait"+userid.getText().toString(), 10).show();
                login(userid.getText().toString(),pwd1.getText().toString());
            }

            private void login(String userid1, String pass1) {
                // TODO Auto-generated method stub
                try{
                    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                    con=DriverManager.getConnection(url, user, pwd);
                    st=con.createStatement();
                    String str="select userrole from usermaster where m_loginid='"+userid1+"'and m_password='"+pass1+"'" ;
                    rs= st.executeQuery(str);
                    while(rs.next()){
                    userrole1=rs.getString("userrole");
                    if(userrole1=="admin"){
                        Intent intent=new Intent(MahindraActivity.this,MainActivity.class);
                        startActivity(intent);
                    }

                }
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_mahindra, menu);
        return true;
    }
}
</code></pre>

<p>When Debugging the project value for userrole is giving "admin" from the table but when assigning it to string variable <code>String userrole1</code> it gives null at the line </p>

<pre><code>while(rs.next()){
                    userrole1=rs.getString("userrole");
                    if(userrole1=="admin"){
                        Intent intent=new Intent(MahindraActivity.this,MainActivity.class);
                        startActivity(intent);
                    }
</code></pre>

<p>If loop is skipped and goes to the catch part, Can some one please explain the reason or spot out the error</p>
