當我嘗試使用空的用戶名和密碼測驗我的添加用戶方法時。它仍然在沒有用戶名和密碼的情況下將用戶添加到我的資料庫中。我如何檢查我的用戶名 || 密碼為空?
我試過這個:
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(?,?) ";
但這不起作用。
uj5u.com熱心網友回復:
您的字串可能是“空”字串或包含可能導致失敗的空格(即空格)。
而是使用類似的東西:
if(userName == null || userName.isBlank())
{
throw new IllegalArgumentException("username can't be empty");
}
// repeat test for password
uj5u.com熱心網友回復:
你可以這樣嘗試
if(userName == null || password == null || userName.isBlank() || password.isBlank())
{
throw new IllegalArgumentException("Username or Password can't be empty");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/469977.html
上一篇:當我想從樹集中洗掉物件時,為什么會出現不受支持的操作例外
下一篇:Haskell中的“修復”是什么?為什么“修復錯誤”會列印一個無限的字串?為什么“take10$fixerror”也一樣呢?
