When i try testing my add user method with an empty username and password. It still adds the users to my database without username and password. How can i check if my username || password is empty?
I tried this:
public boolean addUser(String userName, String password) throws Exception {
//Checking if the username or password are empty
if(userName == null || password == null) {
throw new IllegalArgumentException("username or password can't be empty");
}
String sql = "insert into user(username,password) values(?,?) ";
But this doesn't work.
CodePudding user response:
Your string could be the "empty" string or contain white space (ie spaces) which could cause it to fail.
Instead use something like:
if(userName == null || userName.isBlank())
{
throw new IllegalArgumentException("username can't be empty");
}
// repeat test for password
CodePudding user response:
This is the perfect solution which will prevent spaces also:
if(userName == null || password == null || password.trim().isEmpty() || username.trim().isEmpty()) {
throw new IllegalArgumentException("username or password can't be empty");
}