0

I have a database and it has a table user which consists of many attributes like name ,email,etc. Also it has username and password in VARCHAR datatype. I wrote two classes LoginModel and Controller. controller has method loginit through which it checks if login method from LoginModel returns true or not. I am not able to find out why LoginModel always returns false. Please help.

Here are my methods :

from class LoginModel

public boolean login(String userid, String userpass) throws SQLException {
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    String query = "SELECT * FROM user WHERE username = ? AND password = ?";
    try{
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1,userid);
        preparedStatement.setString(2,userpass);
        resultSet = preparedStatement.executeQuery();
        if(resultSet.next()){
            return true;
        }
        else {
           // System.out.println(resultSet.next());
            return false;
        }
    }
    catch (Exception e){
        return false;
    }
    finally {
        preparedStatement.close();
        resultSet.close();
        return false;
    }
}

from class Controller

public void loginit() throws SQLException {
    String userid = uid.getText();
    String userpass = upass.getText();
    try {
        if (loginmodel.login(userid, userpass)) {
            lblstatus.setText("YES");
        }
        else
            lblstatus.setText("Invalid");
    } catch (SQLException e) {
        lblstatus.setText("No");
        e.printStackTrace();
    }

}

It is a JavaFX application. userid and userpass are ids of text-field and password-field respectively. lblstatus is label which always shows Invalid, can't figure out why!

Here is snapshot of my database

enter image description here

minigeek
  • 2,766
  • 1
  • 25
  • 35
  • I think you get en exception in your SQL code. printout the stacktrace and you will see what is happend – Jens Mar 26 '17 at 07:31
  • NO, in case of exception it prints out `NO` which is why I put `Invalid` in else loop. Else it will print stacktrace – minigeek Mar 26 '17 at 07:32
  • NO!!. You catch the exception` catch (Exception e){ return false; }` and return `false` – Jens Mar 26 '17 at 07:33
  • Ohh ohk in loginmodel. let me check. btw i renamed password to passkey coloumn still same . – minigeek Mar 26 '17 at 07:34

1 Answers1

1

remove return false from the finally block. It will executed in case of execption and in case you get no exception.

finally {
        preparedStatement.close();
        resultSet.close();
        return false;
    }

Change your code to:

public boolean login(String userid, String userpass) throws SQLException {
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    String query = "SELECT * FROM user WHERE username = ? AND password = ?";
    try{
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1,userid);
        preparedStatement.setString(2,userpass);
        resultSet = preparedStatement.executeQuery();
        if(resultSet.next()){
            return true;
        }
        else {
           // System.out.println(resultSet.next());
            return false;
        }
    }
    catch (Exception e){
        return false;
    }
    finally {
        preparedStatement.close();
        resultSet.close();
    }
       return false;

}
Jens
  • 67,715
  • 15
  • 98
  • 113