简体   繁体   English

Java JDBC数据库连接登录

[英]Java JDBC database connection login

I am trying to make an android application which can login with users from the database so far it gets the users and it gives me a toast when I can log it in. right now it checks every single user so it shows toasts for every user for example when I put in a valid username and password it will first say wrong username or password and after logging in.. .I know it is because of the while loop but I don't know how to make it show once when the username and password is wrong. 我正在尝试制作一个可以从数据库中与用户登录的android应用程序,到目前为止它能吸引用户,并且在我可以登录时给我敬酒。现在它检查每个用户,以便为每个用户显示敬酒例如,当我输入有效的用户名和密码时,登录时将首先输入错误的用户名或密码。..我知道这是因为while循环,但是当用户名和密码输入时,我不知道如何使它显示一次密码错误。 This is my code 这是我的代码

private void setupMessageButton() {

    button = (Button) findViewById(R.id.button1);
    text = (TextView) findViewById(R.id.editText1);
    text2 = (TextView) findViewById(R.id.editText2);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub  
            textje = text.getText().toString();
            textje2 = text2.getText().toString();

            try {

                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://dbhost", "root", "dbpass");

                Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
                ResultSet rs = stmt.executeQuery("SELECT * FROM vtiger_users");
                while(rs.next()) {

                    users = rs.getObject(2).toString();
                    wachtwoorden = rs.getObject(3).toString();
                    if(textje.equals(users) && textje2.equals(wachtwoorden)) {

                        Toast.makeText(getApplicationContext(), "Logging in..", Toast.LENGTH_SHORT).show();
                    } else {

                        Toast.makeText(getApplicationContext(), "Wrong username or password", Toast.LENGTH_SHORT).show();
                    }
                }
                con.close();

            } catch(Exception e) {

                e.printStackTrace();
            }
        }
    });
}

Update your query like.. 更新您的查询。

SELECT * FROM vtiger_users where user=#user# and password=#pass# 选择* FROM vtiger_users,其中user =#user#和password =#pass#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM