iT邦幫忙

0

Spring Boot 後端問題(2)

  • 分享至 

  • xImage

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 好想哭

pic

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
kaijunisme
iT邦新手 2 級 ‧ 2021-12-25 00:38:54

if 的判斷式
a.get(i).getUName() == username
改成
username.equals(a.get(i).getUName())
試試看

OscarCS iT邦新手 5 級 ‧ 2021-12-25 11:00:53 檢舉

我今天早上自己寫出來啦 謝謝你的回答 /images/emoticon/emoticon41.gif
我最後改成

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;
             }

        }
    }

我要發表回答

立即登入回答