gpt4 book ai didi

java - 如何从数据库保存登录信息

转载 作者:行者123 更新时间:2023-11-29 10:41:04 25 4
gpt4 key购买 nike

我正在创建非常简单的 ATM 机。

一开始,系统会询问用户登录名和密码。如果密码正确则用户可以进行一些操作。

现在它有如下操作:1)提款2)存钱3)当前值

问题是,每次我选择 1) 或 2) 或 3) 时,程序都会再次询问我有关登录名和密码的信息。但我只想在开始时被问一次。

所以我需要解决方案,它允许我将用户登录名/密码保存在某处,并在我想做一些操作时读取它。

这是我的代码的一部分。

public class LoginVerification {

public static void login() throws Exception {

Scanner input = new Scanner(System.in);
System.out.println("Please enter USER ID:");
String userId = input.nextLine();
System.out.println("Please enter PIN CODE:");
String pass = input.nextLine();

try {
Connection con = ConnectionDB.getConnection();
String sql = "SELECT COUNT(*) FROM Info WHERE ClientID = ? AND ClientPass = ?";

PreparedStatement posted = con.prepareStatement(sql);
posted.setString(1, userId);
posted.setString(2, pass);
ResultSet resultSet = posted.executeQuery();
resultSet.next();
int rowCount = resultSet.getInt(1);

if (rowCount == 1) {
System.out.println("LOGIN SUCCESSFUL");
Helper.showMainMenu();

} else {
System.out.println("#LOGIN UNSUCCESSFUL, WRONG LOGIN OR PASSWORD");

System.out.println("\nTry again? \n1) yes \n2) no [quit]");
int choice = input.nextInt();
if (choice == 1) {
login();
} else {
System.exit(0);
}
}
con.close();

} catch (Exception e) {
e.printStackTrace();
}

}

}

问题如下。正如您所看到的,程序再次询问有关登录/密码的信息,因为我不知道如何以不同的方式做到这一点。

public static void withdraw() throws Exception {

Scanner input = new Scanner(System.in);
System.out.println("please enter USER ID:");
String userId = input.nextLine();
System.out.println("please enter PIN CODE:");
String pass = input.nextLine();

System.out.println("how much you want to withdraw");
int money = input.nextInt();

try {
Connection con = ConnectionDB.getConnection();
String sql = "UPDATE BankDB.Info SET `Money`= `Money` - ? WHERE `ClientID`= ? AND `ClientPass` = ?";

PreparedStatement posted = con.prepareStatement(sql);
posted.setInt(1, money);
posted.setString(2, userId);
posted.setString(3, pass);

if (posted.executeUpdate() > 0) {
System.out.println("OPERATION SUCCESSFUL");
} else {
System.out.println("LOGIN UNSUCCESSFUL");

System.out.println("\nTry again? \n1) yes \n2) no [quit]");
int choice = input.nextInt();
if (choice == 1) {
withdraw();
} else {
System.exit(0);
}
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

你需要一个标志来判断用户是否登录,这可以在 login() 中设置,并且可以从任何页面检查/使用

修改login()如下:

1- 在 LoginVerification 类中定义类范围变量

private static boolean loggedIn = false;

login() 中的第一行,检查该标志

public static void login() throws Exception {

if(loggedIn){
//do nothing, user already logged in
return;
}
//...rest of code ...

当您发现用户登录正确与否时添加此内容:

if (rowCount == 1) {
//add this line
loggedIn = true;
} else {
//add this line
loggedIn = false;
}

现在在需要用户登录的所有其他地方,使用

LoginVerification.login();

例如,替换此

public static void withdraw() throws Exception {

Scanner input = new Scanner(System.in);
System.out.println("please enter USER ID:");
String userId = input.nextLine();
System.out.println("please enter PIN CODE:");
String pass = input.nextLine();

System.out.println("how much you want to withdraw");// rest of code

这样:

public static void withdraw() throws Exception {
LoginVerification.login();
System.out.println("how much you want to withdraw"); // rest of code

如果用户尚未登录,系统将要求用户登录。

关于java - 如何从数据库保存登录信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45477438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com