使用Java构建可视化登陆注册界面,链接MySQL数据库,给出具体实现代码
时间: 2024-03-14 13:43:10 浏览: 18
好的,下面是具体实现代码,我将分别给出登录和注册表单的代码:
登录表单:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class LoginForm extends JFrame implements ActionListener {
private JLabel titleLabel, usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private Connection connection;
public LoginForm() {
// 设置窗体标题
setTitle("登录");
// 创建组件
titleLabel = new JLabel("登录");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
usernameLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
loginButton = new JButton("登录");
// 设置布局
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel());
panel.add(loginButton);
add(titleLabel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
// 添加监听器
loginButton.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
// 设置窗体属性
setSize(400, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM Users WHERE username='" + username + "' AND password='" + password + "'");
if (resultSet.next()) {
JOptionPane.showMessageDialog(this, "登录成功!");
dispose();
// 进入应用程序主界面
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new LoginForm();
}
}
```
注册表单:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class RegisterForm extends JFrame implements ActionListener {
private JLabel titleLabel, usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton registerButton;
private Connection connection;
public RegisterForm() {
// 设置窗体标题
setTitle("注册");
// 创建组件
titleLabel = new JLabel("注册");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
usernameLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
registerButton = new JButton("注册");
// 设置布局
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel());
panel.add(registerButton);
add(titleLabel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
// 添加监听器
registerButton.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
// 设置窗体属性
setSize(400, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == registerButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM Users WHERE username='" + username + "'");
if (resultSet.next()) {
JOptionPane.showMessageDialog(this, "用户名已存在!");
} else {
statement.executeUpdate("INSERT INTO Users (username, password) VALUES ('" + username + "', '" + password + "')");
JOptionPane.showMessageDialog(this, "注册成功!");
dispose();
// 进入应用程序主界面
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new RegisterForm();
}
}
```
以上代码仅供参考,你可以根据实际需求进行修改和优化。