简体   繁体   中英

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. 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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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