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

2023-09-08 09:13:17 作者:収敛惆怅

我设法创建该函数的逻辑,但我不知道如何与数据库相匹配。我只知道如何做到这一点的硬编码方式。

下面是我做到了。谁能帮我,我相信一个SQL语句应该插入那里就有我右边的客人的一部分?我知道我错过了一些东西。在我匹配用户名和密码,我应该检查用户是否存在于数据库中,然后再检查是否与用户名的密码匹配。

所以在最后,我需要对',如果别人的语句帮助。

 包one.two;进口android.app.Activity;进口android.content.Intent;进口android.database.Cursor;进口android.os.Bundle;进口android.view.View;进口android.view.View.OnClickListener;进口android.widget.Button;进口android.widget.EditText;进口android.widget.GridView;进口android.widget.ListView;进口android.widget.TextView;公共类扩展登录活动实现OnClickListener {/ **当第一次创建活动调用。 * /    私人的EditText etUsername;    私人的EditText etPassword;    私人按钮btnLogin;    //专用按钮btnRegister;    私人TextView的lblResult;    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);     //获取的EditText和Button参考        etUsername =(EditText上)findViewById(R.id.usernametxt);        etPassword =(EditText上)findViewById(R.id.passwordtxt);        btnLogin =(按钮)findViewById(R.id.btnLogin);        // btnRegister =(按钮)findViewById(R.id.btnRegister);        lblResult =(的TextView)findViewById(R.id.msglbl);        按钮btnArrival =(按钮)findViewById(R.id.btnRegister);        btnArrival.setOnClickListener(本);    //设置点击监听器    btnLogin.setOnClickListener(新OnClickListener(){    @覆盖    公共无效的onClick(视图v){        //检查登录        字符串username = etUsername.getText()的toString()。        。字符串密码= etPassword.getText()的toString();        如果(username.equals(客户)及和放大器; password.equals(客户)){            lblResult.setText(登录成功);        }其他{            lblResult.setText(登录失败的用户名和/或密码不匹配。);        }    }});    }    公共无效的onClick(视图v)    {            意向意图=新意图(这一点,Da​​tabaseActivity.class);            startActivity(意向);}} 

解决方案   

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

账户无法登录,网页自动弹出用户名和密码不匹配

没有,除非它在你的要求,你不需要进行两次的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