Java 如何从数据库保存登录信息
我正在创造非常简单的ATM机 开始时,会询问用户登录名和密码。 若密码正确,那个么用户可以进行一些操作 现在它有如下操作: 1取款 2存款 3当前值 问题是,每当我选择1、2或3时,程序都会再次询问我有关登录名和密码的问题。但一开始我只想被问一次 所以,我需要一个解决方案,它允许我保存用户登录名/密码,并在我想做一些操作时读取它 这是我代码的一部分Java 如何从数据库保存登录信息,java,mysql,jdbc,Java,Mysql,Jdbc,我正在创造非常简单的ATM机 开始时,会询问用户登录名和密码。 若密码正确,那个么用户可以进行一些操作 现在它有如下操作: 1取款 2存款 3当前值 问题是,每当我选择1、2或3时,程序都会再次询问我有关登录名和密码的问题。但一开始我只想被问一次 所以,我需要一个解决方案,它允许我保存用户登录名/密码,并在我想做一些操作时读取它 这是我代码的一部分 public class LoginVerification { public static void login() throws Except
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();
}
}
您需要一个标志来告诉用户是否登录, 这可以在登录中设置,并且可以从任何页面检查/使用 修改登录名,如下所示: 1-在LoginVerification类中定义类范围变量 登录的第一行,检查该标志
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
如果用户尚未登录,将要求用户登录。使用标志并在必要时进行检查登录后,您需要为该用户维护会话。即通过设置布尔变量。对于所有操作,您需要检查该变量是否为真。但是看看你的代码,看起来你做得不对。在类上有userid和pass-as字段,而不是局部变量,然后你可以检查它们是否在某些场景中被设置,在实际操作之前登录是个好主意,甚至是必需的。假设用户已经登录,然后分心,其他人可以访问操作?一些自动取款机要求在交易请求后输入pin码,基于芯片的卡会在没有通知的情况下被查询。但您的问题在于,您看到在登录后关闭会话。连接是活动登录的生命周期。谢谢你们的提示。你能给我一个简短的代码示例来解决这个问题吗?我刚开始编程,很难在实践中运用你的建议。
public static void withdraw() throws Exception {
LoginVerification.login();
System.out.println("how much you want to withdraw"); // rest of code