可能重复:
为什么我的字符串比较失败?
我无法存储为字符串变量赋值,我在下面给出了我的程序
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;
}
}
当调试用户角色的项目值时,从表中给出“admin”,但是当将它分配给字符串变量String userrole1
时,它在该行给出 null
while(rs.next()){
userrole1=rs.getString("userrole");
if(userrole1=="admin"){
Intent intent=new Intent(MahindraActivity.this,MainActivity.class);
startActivity(intent);
}
如果loop被跳过并进入catch部分,请有人解释原因或找出错误