创建一个Java GUI界面,包含两个选项:注册和登录。 当用户点击注册选项时,弹出一个新的窗口,要求用户输入用户名和密码。将这些信息存储到mysql数据库中的用户表中。
时间: 2024-05-05 16:17:41 浏览: 144
Java+mysql用户注册登录功能
当用户点击登录选项时,弹出一个新的窗口,要求用户输入用户名和密码。从mysql数据库中的用户表中查询该用户是否存在,如果存在则跳转到主页面,否则弹出错误提示。
以下是示例代码:
Main.java
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Login/Register");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LoginDialog loginDialog = new LoginDialog(frame);
loginDialog.setVisible(true);
}
});
panel.add(loginButton);
JButton registerButton = new JButton("Register");
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RegisterDialog registerDialog = new RegisterDialog(frame);
registerDialog.setVisible(true);
}
});
panel.add(registerButton);
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
LoginDialog.java
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class LoginDialog extends JDialog {
private JTextField usernameField;
private JPasswordField passwordField;
public LoginDialog(Frame parent) {
super(parent, "Login", true);
JPanel panel = new JPanel(new GridLayout(2, 2));
JLabel usernameLabel = new JLabel("Username:");
panel.add(usernameLabel);
usernameField = new JTextField();
panel.add(usernameField);
JLabel passwordLabel = new JLabel("Password:");
panel.add(passwordLabel);
passwordField = new JPasswordField();
panel.add(passwordField);
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM users WHERE username='" + username + "' AND password='" + password + "'");
if (resultSet.next()) {
JOptionPane.showMessageDialog(LoginDialog.this, "Login successful!");
dispose();
// TODO: Jump to main page
} else {
JOptionPane.showMessageDialog(LoginDialog.this, "Invalid username or password!", "Error", JOptionPane.ERROR_MESSAGE);
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
panel.add(loginButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
panel.add(cancelButton);
add(panel);
pack();
setLocationRelativeTo(parent);
}
}
```
RegisterDialog.java
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class RegisterDialog extends JDialog {
private JTextField usernameField;
private JPasswordField passwordField;
public RegisterDialog(Frame parent) {
super(parent, "Register", true);
JPanel panel = new JPanel(new GridLayout(2, 2));
JLabel usernameLabel = new JLabel("Username:");
panel.add(usernameLabel);
usernameField = new JTextField();
panel.add(usernameField);
JLabel passwordLabel = new JLabel("Password:");
panel.add(passwordLabel);
passwordField = new JPasswordField();
panel.add(passwordField);
JButton registerButton = new JButton("Register");
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(RegisterDialog.this, "Username and password cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM users WHERE username='" + username + "'");
if (resultSet.next()) {
JOptionPane.showMessageDialog(RegisterDialog.this, "Username already exists!", "Error", JOptionPane.ERROR_MESSAGE);
} else {
statement.executeUpdate("INSERT INTO users (username, password) VALUES ('" + username + "', '" + password + "')");
JOptionPane.showMessageDialog(RegisterDialog.this, "Registration successful!");
dispose();
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
panel.add(registerButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
panel.add(cancelButton);
add(panel);
pack();
setLocationRelativeTo(parent);
}
}
```
阅读全文