Java写一个登录注册源代码,要求连接数据库,有登录,注册,忘记密码,注册要求输入两次密码,忘记密码要求选择忘记密码后,输入登录过的账号,直接输入两次密码
时间: 2023-12-11 16:04:52 浏览: 137
利用JDBC实现注册用户账号和登录功能Android源码
这是一个基于Java Swing和MySQL数据库的简单登录注册应用程序的源代码,包括登录,注册,忘记密码功能:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Login implements ActionListener {
JFrame frame;
JLabel userLabel, passLabel, errorLabel, pass2Label;
JTextField userField;
JPasswordField passField, pass2Field;
JButton loginButton, registerButton, forgotButton;
JPanel panel;
Connection con;
PreparedStatement ps;
Login() {
frame = new JFrame("Login/Register/Forgot Password");
frame.setBounds(100, 100, 350, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
userLabel = new JLabel("Username:");
userLabel.setBounds(50, 30, 100, 25);
panel.add(userLabel);
userField = new JTextField();
userField.setBounds(150, 30, 150, 25);
panel.add(userField);
passLabel = new JLabel("Password:");
passLabel.setBounds(50, 65, 100, 25);
panel.add(passLabel);
passField = new JPasswordField();
passField.setBounds(150, 65, 150, 25);
panel.add(passField);
errorLabel = new JLabel("");
errorLabel.setBounds(50, 100, 250, 25);
errorLabel.setForeground(Color.RED);
panel.add(errorLabel);
loginButton = new JButton("Login");
loginButton.setBounds(50, 135, 100, 25);
loginButton.addActionListener(this);
panel.add(loginButton);
registerButton = new JButton("Register");
registerButton.setBounds(150, 135, 100, 25);
registerButton.addActionListener(this);
panel.add(registerButton);
forgotButton = new JButton("Forgot Password");
forgotButton.setBounds(50, 170, 200, 25);
forgotButton.addActionListener(this);
panel.add(forgotButton);
pass2Label = new JLabel("Confirm Password:");
pass2Label.setBounds(50, 100, 120, 25);
pass2Label.setVisible(false);
panel.add(pass2Label);
pass2Field = new JPasswordField();
pass2Field.setBounds(170, 100, 130, 25);
pass2Field.setVisible(false);
panel.add(pass2Field);
frame.setVisible(true);
// 连接到MySQL数据库
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "");
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
public void actionPerformed(ActionEvent e) {
// 点击登录按钮
if (e.getSource() == loginButton) {
String username = userField.getText();
String password = new String(passField.getPassword());
try {
// 查询用户是否存在
ps = con.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
// 登录成功
errorLabel.setText("");
JOptionPane.showMessageDialog(frame, "Login successful!");
} else {
// 登录失败
errorLabel.setText("Invalid username or password.");
}
} catch (SQLException ex) {
System.out.println("Error: " + ex.getMessage());
}
}
// 点击注册按钮
if (e.getSource() == registerButton) {
String username = userField.getText();
String password = new String(passField.getPassword());
String password2 = new String(pass2Field.getPassword());
if (!password.equals(password2)) {
// 两次密码不一致
errorLabel.setText("Passwords do not match.");
} else {
try {
// 插入新用户
ps = con.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
ps.setString(1, username);
ps.setString(2, password);
int result = ps.executeUpdate();
if (result != 0) {
// 注册成功
errorLabel.setText("");
JOptionPane.showMessageDialog(frame, "Registration successful!");
} else {
// 注册失败
errorLabel.setText("Registration failed.");
}
} catch (SQLException ex) {
System.out.println("Error: " + ex.getMessage());
}
}
}
// 点击忘记密码按钮
if (e.getSource() == forgotButton) {
String username = JOptionPane.showInputDialog(frame, "Enter username:");
if (username != null) {
String password = new String(JOptionPane.showInputDialog(frame, "Enter new password:").toCharArray());
String password2 = new String(JOptionPane.showInputDialog(frame, "Confirm new password:").toCharArray());
if (!password.equals(password2)) {
// 两次密码不一致
errorLabel.setText("Passwords do not match.");
} else {
try {
// 更新用户密码
ps = con.prepareStatement("UPDATE users SET password = ? WHERE username = ?");
ps.setString(1, password);
ps.setString(2, username);
int result = ps.executeUpdate();
if (result != 0) {
// 更新密码成功
errorLabel.setText("");
JOptionPane.showMessageDialog(frame, "Password updated!");
} else {
// 更新密码失败
errorLabel.setText("User not found.");
}
} catch (SQLException ex) {
System.out.println("Error: " + ex.getMessage());
}
}
}
}
}
public static void main(String[] args) {
new Login();
}
}
```
这个应用程序使用了Java Swing库来创建GUI界面,使用了MySQL数据库,可以通过更改连接字符串的参数来连接到其他数据库。
注意:此示例代码仅用于学习和演示目的,不应在生产环境中使用。在实际应用程序中,需要实现更多的安全保护措施,如加密密码等。
阅读全文