How to avoid blank entry into signup textfields which stores data in mysql db in Java? How to avoid logging in without filling username and password?

Viewed 52

I am confused by the two questions on the title. What function do you recommend me to use in the following controller class that avoids blank entries in SignUp and makes it impossible to log in with null or "" values without passing authentication? I examined different ways but it either didn't work at all or gave some error. The application works well otherwise. I use an online Mysql and it works well.

    @FXML
    void fcnBtnSignUp(ActionEvent event) {
        
        
        
        try {
            
            conn = dbConnection.connect();
            
            String sql = "insert into tblSignUp (FULL_NAME, GENDER, EMAIL, PASSWORD, RE_PASSWORD) values (?,?,?,?,?)";
     
            
            pst= conn.prepareStatement(sql);
            
            pst.setString(1, txtFullName.getText());
            pst.setString(2, txtGender.getText());
            pst.setString(3, txtEmail.getText());
            pst.setString(4, txtPassword.getText());
            pst.setString(5, txtRePassword.getText());
            
            pst.execute();
            
            txtFullName.setText("");
            txtGender.setText("");
            txtEmail.setText("");
            txtPassword.setText("");
            txtRePassword.setText("");
                
            
        
                    Parent root;
                    try {
                        
                        Stage stage = (Stage) btnSignUp.getScene().getWindow();
                        // do what you have to do
                        stage.close();
                        
                        root = FXMLLoader.load(getClass().getResource("SignUpSuccess.fxml"));
                        Scene s = new Scene(root);
                        Stage ss = new Stage();
                        ss.setScene(s);
                        ss.show();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            
            
        } catch (SQLException ex) {
            Logger.getLogger(SampleController.class.getName()) . log(Level.SEVERE, null,ex);

        }

}

    @FXML
    void fcnBtnLogin(ActionEvent event) {
    
    String a ="";
    
    
    try {
        
        conn = dbConnection.connect();
        
        String sql = "Select * from tblSignUp where EMAIL=? AND PASSWORD=?";
        
        
        pst =conn.prepareStatement(sql);
        pst.setString(1, txtLoginEmail.getText());
        pst.setString(2, txtLoginPassword.getText());
        

        
        
        
        
        
        ResultSet rs = pst.executeQuery();
        
        
        if(rs.next() == true) {
            
            
            try {
                
                Stage stage = (Stage) btnLogin.getScene().getWindow();
                // do what you have to do
                stage.close();
                Parent root = FXMLLoader.load(getClass().getResource("ReadingMain.fxml"));
                
                Scene s = new Scene(root);
                Stage ss = new Stage();
                ss.setScene(s);
                ss.show();
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
        }else {
            System.out.println("Login Unsuccessful! Incorrect Username or Password");
        }
        
        
        
        
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    
}
3 Answers

NOT NULL

Did you try during the creation of the database table to insert the property "NOT NULL" to the fields?

e.g.

CREATE user profile {
FULL_NAME VarChar(20) NOT NULL
GENDER VarChar(10) NOT NULL
EMAIL VarChar(30) NOT NULL
PASSWORD_HASH BINARY(64) NOT NULL
}

This prevents the insertion of null values upstream, and should obviously only be placed where it is needed.

For something as important as authentication credentials you should put checks on both the client side and the server side.

Client side

I’m not an expert on JavaFX, but according to this post, you need to simply check the text contents of your data-entry fields.

Here is my modified version of that code. I would use String#isBlank rather than isEmpty to test for all whitespace in addition to testing for no text at all.

Label response = new Label();
Button submit = new Button( "Submit" );
TextField username = new TextField();
submit.setOnAction( action -> 
    {
        if ( username.getText().isBlank() )
        {
            response.setText( "The “User Name” field is required." );
        }
        else 
        {
            response.setText( "The name you entered is " + username.getText() );
            … 
        } 
    }
);

Server side

On the database server, do as instructed in the correct Answer by Eugenio. Set NOT NULL in columns that should never be empty.

I would also add a trigger on those columns to verify incoming values are not empty/blank strings. (A NULL and a empty string are two different things.)

Simply put, from the java side you need to validate that your textfields and passwordfields aren't empty by using the isEmpty() method from the String class. Documentation can be found HERE

This method returns a boolean, and you can execute the rest of your code once data validation is done.

Oversimplified Example:

@FMXL fcnBtnSignUp(ActionEvent e) {
    // Check if the fields are empty, if they are, warn the user else continue

    if(txtFullName.getText().isEmpty() || txtGender.getText().isEmpty() || 
    txtEmail.getText().isEmpty() || txtPassword.getText().isEmpty() || 
    txtRePassword.getText().isEmpty()) {

    // Here you can set a send a popup to the user or have a label display "All 
    fields must be filled to continue"

    } else {

    // Now that you validated that none of the Strings you want to store in your 
     database are null, you can proceed. 

    // here goes your try/catch block and execute your ps statement.
    
    }
}

Another thing, you should use passwordFields for passwords.. Documentation HERE

Lastly, think about using encryption if you are going to handle user credentials and store passwords in a database. BCrypt is a good library, documentation HERE

For the login method, it's the same thing.You validate that the fields aren't empty using the same method, then compare the entered Strings with what is stored in the database.

Related