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();
}
}
CodePudding user response:
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.
CodePudding user response:
For something as important as authentication credentials you should put checks on both the client and 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();
button.setOnAction( e ->
{
if ( username.getText().isBlank() )
{
response.setText( "The “User Name” field is required." );
}
else
{
response.setText( "The name you entered is " text.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.
