hi 又是我
我還沒做登入 因為我註冊後面又遇到一點障礙QQ
有問題的程式如下:
Controller類
@RestController
public class accountController {
@Autowired
private accountRegister accountRegisterManager;
@PostMapping("/register")
@ResponseBody
public int register(@RequestParam("username")String username, @RequestParam("password")String password){
int result = 1;
List<account> a = accountRegisterManager.findall() ;
for (int i=0; i < a.size() ; i++){
if (a.get(i).getUName() == username){
result = 0 ;
}
}
if(result == 1){
accountRegisterManager.insertAccount(username,password);
}
return result;
}
}
Service類
@Service
public class accountRegister {
@Autowired
private Sql2oDbHandler sql2oDbHandler;
private account account;
public accountRegister() {
}
public List<account> findall (){
String query = "select Account_ID , User_name , User_password "
+ " from account ";
try (Connection con = sql2oDbHandler.getConnector().open()) {
return con.createQuery(query)
.executeAndFetch(account.class) ;
}
}
public void insertAccount( String username, String password){
String insertQuery =
"INSERT INTO account ( User_name, User_password) "
+ "VALUES (:username, :user_password)";
try (Connection con = sql2oDbHandler.getConnector().open()) {
con.createQuery(insertQuery)
.addParameter("username",username )//account.getUName()
.addParameter("user_password",password) //account.getPassword()
.executeUpdate();
}
}
}
我遇到的問題是為什麼我的這行:
for (int i=0; i < a.size() ; i++){
if (a.get(i).getUName() == username){
result = 0 ;
}
}
無法執行?
導致 html return 永遠是1
然後我的資料庫 一堆一樣的username password 好想哭
if 的判斷式a.get(i).getUName() == username
改成username.equals(a.get(i).getUName())
試試看
我今天早上自己寫出來啦 謝謝你的回答
我最後改成
public boolean find_same_username (String username){
String query = "select Account_ID , User_name , User_password"
+ " from account where User_name = :username";
List<account> accounts ;
try (Connection con = sql2oDbHandler.getConnector().open()) {
accounts = con.createQuery(query)
.addParameter("username", username)
.executeAndFetch(account.class);
if(accounts!= null & accounts.size()>0 ){
return true;
}
else{
return false;
}
}
}