Search code examples
javasqldatabaseeclipseresultset

Java SQL Query Resultset always returns null can't find solution


I am trying to check if a user entered Username and password matches one in my data base however what ever I try the result set still comes up null. the sql varible is set to the username and the pass is set to the password however when ever i enter the correct details it shows up with no results

public boolean Login(){
    boolean valid = true;
        try {
            String stmt = "SELECT * From TBLUser where User = ? and Password = ? ;";
            PreparedStatement pstmt = conn.prepareStatement(stmt);
            pstmt.setString(1, sql); pstmt.setString(2, pass);
            ResultSet rs = pstmt.executeQuery();
            if(!rs.next()){
                valid = false;
            }
        } catch (SQLException e) {
            System.err.println("Error: "+e);
        }
    return valid;
}

Solution

  • The problem with the application is not the code but the database as User one of the column names is a reserved word so this change fixed the problem thanks to @Grayson for all the help

    public boolean Login(){
    boolean valid = true;
        try {
            String stmt = "SELECT * From TBLUser where UserName = ? and Password = ? ;";
            PreparedStatement pstmt = conn.prepareStatement(stmt);
            pstmt.setString(1, sql); pstmt.setString(2, pass);
            ResultSet rs = pstmt.executeQuery();
            if(!rs.next()){
                valid = false;
            }
        } catch (SQLException e) {
            System.err.println("Error: "+e);
        }
    return valid;
    

    }