PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 5, 2022

[FIXED] How to avoid adding empty username and password to my database?

 August 05, 2022     exception, java, junit, sql, unit-testing     No comments   

Issue

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.


Solution

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


Answered By - camickr
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing