我正在創建非常簡單的ATM機。我有MySQL的數據庫像列:loginID,密碼,錢。我想改變我的方法,可以在開始時驗證登錄名和密碼的正確性(並在此之後做一些事情)。因此,如果用戶名和密碼是正確的,那麼我想收到消息「登錄成功」或登錄失敗」。現在,我一直有消息‘登錄成功’,我怎樣才能改變呢?如何驗證登錄名/密碼的正確性JDBC
public static void post() throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println ("please enter user id:");
String userId = sc.nextLine();
System.out.println("please enter password:");
String pass = sc.nextLine();
System.out.println("how much you want to put");
int money = sc.nextInt();
try {
Connection con = ConnectionDB.getConnection();
String sql = "UPDATE `BankDB`.`Info` SET `Money`= ? WHERE `ClientID`= ? AND `ClientPass` = ?";
PreparedStatement posted = con.prepareStatement(sql);
posted.setInt(1, money);
posted.setString(2, userId);
posted.setString(3, pass);
posted.executeUpdate();
con.close();
} catch (Exception e) {
e.printStackTrace();
} finally{
System.out.println("Login was succesful");
}
}
無論是否引發異常,finally子句將始終執行。 – FredK