0

Giving error at line 12 "This method must return a result of type Boolean". I have written my code in try catch block. If a move the resultset operation below the catch block then the error appears on resultset object. Where am I wrong, Please answer me. Thank you.

public class LoginService {
    public Boolean verifyLogin(LoginModel loginModel) { // In this line it is
                                                        // giving error
        DbConnection dbConnection = new DbConnection();
        ResultSet rs;
        try {
            Connection con = dbConnection.getConnection();
            System.out.println("Connection Established");
            String query = "select * from login where tenantid=? and userid=? and password=?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setInt(1, loginModel.getTenantid());
            ps.setString(2, loginModel.getUserid());
            ps.setString(3, loginModel.getPassword());
            rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("User exists !!");
                return true;
            } else {
                System.out.println("User does not exists !!");
                return false;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
1
  • 5
    What happens if an exception occurs? What value does your method return then? Commented Mar 6, 2014 at 18:29

6 Answers 6

1

When your code catches an exception you are simply printing the stacktrace and then allowing the function to continue.

However, after the catch blocks, you have no return statement, which is what the complaint is.

Sign up to request clarification or add additional context in comments.

Comments

1

As others have mentioned, you're missing a return statement either in the catch blocks or after the catch blocks. Here's my boiler plate example of a function that returns a boolean:

bool foo()
{
    bool result = false;

    //do stuff, and set result to true at some point

    return result;
}

This pattern is beneficial because it helps reduce the number of returns in your functions. There are some coding styles out there that won't allow more than 2 return statements in a function, for example.

Here it is applied to your function:

public Boolean verifyLogin(LoginModel loginModel) { // In this line it is
                                                    // giving error
    Boolean result = false;
    DbConnection dbConnection = new DbConnection();
    ResultSet rs;
    try {
        Connection con = dbConnection.getConnection();
        System.out.println("Connection Established");
        String query = "select * from login where tenantid=? and userid=? and password=?";
        PreparedStatement ps = con.prepareStatement(query);
        ps.setInt(1, loginModel.getTenantid());
        ps.setString(2, loginModel.getUserid());
        ps.setString(3, loginModel.getPassword());
        rs = ps.executeQuery();
        if (rs.next()) {
            System.out.println("User exists !!");
            result = true; //--------------------This line changed!
        } else {
            System.out.println("User does not exists !!");
            result = false; //-------------------This line changed!
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

Comments

0

You should return in catch blocks too. Or in finally block.

Comments

0

You need to add a return statement after your catch block.

Comments

0

Your method may throw an exception, in this case, the code that will be executed will be in the catch clauses.

Add a finally clause after the catch's with the desired value for when this happens.

Comments

0

The problem is that if an exception occurs, nothing is returned. I would suggest adding

return false;

to all of your catch blocks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.