且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RE:登录页面。如何从数据库匹配用户名和密码,允许用户登录?

更新时间:2023-12-03 19:56:16


  

在我匹配用户名和密码,我应该检查用户是否存在于数据库中,然后再检查是否与用户名的密码匹配。


块引用>

没有,除非它在你的要求,你不需要进行两次的SQL。更好的你做这样的事情。 从tableName值SELECT COUNT(*),其中userNameField =用户名和passwordField =密码并检查是否行数>大于0登录为好,否则用户名或密码无效

I managed to create the logic for the function but I have no idea how to match it with the database. I only know how to do it the hardcoding way.

Below is how I did it. Can anyone help me with the 'guest' part which I believe an sql statement should be inserted there am I right? And I know I missed out something. Before I match the username and password, I should check whether the user exist in the database first, and then check whether the password match with the username.

So in conclusion I need help on the 'if else' statement.

package one.two;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;



public class Login extends Activity implements OnClickListener{

/** Called when the activity is first created. */

    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    //private Button btnRegister;
    private TextView lblResult;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
     // Get the EditText and Button References
        etUsername = (EditText)findViewById(R.id.usernametxt);
        etPassword = (EditText)findViewById(R.id.passwordtxt);
        btnLogin = (Button)findViewById(R.id.btnLogin);
        //btnRegister = (Button)findViewById(R.id.btnRegister);
        lblResult = (TextView)findViewById(R.id.msglbl);


        Button btnArrival = (Button) findViewById(R.id.btnRegister);
        btnArrival.setOnClickListener(this);


    // Set Click Listener
    btnLogin.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // Check Login
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();

        if(username.equals("guest") && password.equals("guest")){
            lblResult.setText("Login successful.");
        } else {
            lblResult.setText("Login failed. Username and/or password doesn't match.");
        }
    }
});



    }

    public void onClick(View v)
    {
            Intent intent = new Intent(this, DatabaseActivity.class);
            startActivity(intent);
}

}

Before I match the username and password, I should check whether the user exist in the database first, and then check whether the password match with the username.

no unless Its in your requirement you don't need to make two SQLs . Better you do something like. select count(*) from tableName where userNameField = username and passwordField = password and check If the number of rows is > than 0 Login is Okay otherwise username OR Password is invalid