iT邦幫忙

2025 iThome 鐵人賽

0
自我挑戰組

飲食營養追蹤 + 健康分析 Web 系統系列 第 18

使用者登入和註冊

  • 分享至 

  • xImage
  •  

目的

管理使用者資料 , 前面有寫過user和userDAO這裡會用到。

1.歡迎選單(Main)
UserDAO user_dao = new UserDAO(conn) :建立一個UserDAI物件 , 進行連線並叫user_dao。

Integer currentUserId = null : 宣告一個變數currentSerId , 型別是Integer , 預設為null表示目前沒有使用者登入。

while(currentUserId == null) : 只要還沒登入 , 就一直執行裡面的程式。
下面是一些錯誤處理 , 前面解釋過。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845Rr8SD4X1iM.jpg


2.Main
新增2個case 。
case1(註冊) :
while(true) : 如果輸入錯誤不會回到選單 , 會回到輸入名字。

年齡、身高、體重都是設條件為<=0 , 所以在if前將字串轉成int/double。

double height = Double.parseDouble(sc.nextLine().trim()) : 直接把輸入值轉成Double , 就不用多寫一行。

user_dao.addUser() : 呼叫addUser方法 , 丟入參數。
寫錯誤處理方法。

case2(登入) :
Integer userId = user_dao.getUserId(loginName ,loginPass) :呼叫getUserId方法 , 把傳回的值存入變數userId。

if(userId != null) : 如果userId不是null就表示使用者有成功登入或註冊 , 就執行裡面的程式碼。

default : 如果上面所有case不符合 , 就執行這。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845Y3Lpjyi6Ap.jpg
https://ithelp.ithome.com.tw/upload/images/20251015/20178845cZx5gZlCNH.jpg
https://ithelp.ithome.com.tw/upload/images/20251015/20178845zFVjmgqmmq.jpg
https://ithelp.ithome.com.tw/upload/images/20251015/20178845UzLO5LTrVL.jpg


3.UserDAO
addUser方法沒變。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845hgAevkDjEe.jpg

getUserId方法 : 傳入name , password參數。
if(rs.next) :表示有讀到資料 , 回傳userId。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845X6vlpLb79H.jpg


4.結果
登入完會直接跳食物記錄系統。
https://ithelp.ithome.com.tw/upload/images/20251015/20178845vXAWEAekHQ.jpg
https://ithelp.ithome.com.tw/upload/images/20251015/20178845jfUKoBjM6y.jpg


上一篇
查詢日期範圍紀錄與營養功能
下一篇
使用者資料更新功能
系列文
飲食營養追蹤 + 健康分析 Web 系統21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言