Issue
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();
}
}
Solution
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.)
Answered By - Basil Bourque
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)