gpt4 book ai didi

java - 存储用户创建的帐户的用户/密码?

转载 作者:行者123 更新时间:2023-12-01 22:35:38 27 4
gpt4 key购买 nike

我正在开发一个空白程序,该程序将允许用户创建一个帐户,以便他们可以存储其余额和提款/存款。用户输入用户名和密码后,如何存储这些信息以便用户可以登录并查看其余额?我不一定要尝试使其非常安全,我只是希望能够存储帐户信息并能够在以后访问它。

这是他们将在其中创建帐户的类(仍在进行中):

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class CreateAccount extends JFrame implements ActionListener {

JLabel username = new JLabel("Enter your username");
JTextField enterUsername = new JTextField(null, 15);
JLabel password = new JLabel("Enter your password");
JPasswordField enterPassword = new JPasswordField(null, 15);
JLabel passwordConfirm = new JLabel("Confirm your password.");
JPasswordField enterConfirmPassword = new JPasswordField(null, 15);
JButton okButton = new JButton("OK");

public CreateAccount() {

add(username);
add(enterUsername);
add(password);
add(enterPassword);
add(passwordConfirm);
add(enterConfirmPassword);
add(okButton);

okButton.addActionListener(this);

setTitle("New Bank Account Creation");
setVisible(true);
setSize(270, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());

}

@Override
public void actionPerformed(ActionEvent e) {

char[] pass = enterPassword.getPassword();
String passString = new String(pass);
char[] passConfirm = enterConfirmPassword.getPassword();
String passStringConfirm = new String(passConfirm);

String userName = enterUsername.getText();

if (e.getSource() == okButton) {
if (!passString.equals(passStringConfirm) || userName.equals(null) || passString.equals(null) || passStringConfirm.equals(null)) {
JOptionPane.showMessageDialog(null, "Entered an invalid username or password!");
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}
}
}
}

这是我希望将这些帐户存储为的帐户类别:

import javax.swing.JOptionPane;


public class Account {
private String name;
private double balance;

public Account(String name, double balance) {
getName();
getBalance();
this.name = name;
this.balance = balance;

}
String getName() {
return name = JOptionPane.showInputDialog("What is your name?");
}
double getBalance() {
String userBalance = JOptionPane.showInputDialog("How much money would you like to deposit?");
return balance = Double.parseDouble(userBalance);
}
public void setName() {
this.name = name;
}
public void setBalance() {
this.balance = balance;
}

}

最佳答案

恕我直言,你应该使用数据库。对于简单的应用程序,您可以使用嵌入式数据库,例如 HSQL、H2 或 Derby。如果您以后的需求增长,您可以使用完全相同的 API 轻松迁移到 MySQL、MariaDB 或 PostgreSQL。

JDBC 并不是一个非常好的 API,但是非常可移植,但是如果您想要存储多于使用的信息,您可以考虑查看 JPA,它有一些很好的实现,例如 Hibernate、EclipseLink 或 OpenJPA。

我差点忘了,好的做法建议只存储密码的哈希值,而不是纯文本版本......

关于java - 存储用户创建的帐户的用户/密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26912539/

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