Java程序:利用GUI和数据库实现一个注册和登录程序。注册时,应向用户提示“注册成功!”或“id不能重复”;登录时,应向用户提示“登录成功”或“登录失败,重新登录”
时间: 2023-09-09 21:12:33 浏览: 38
下面是一个基于Java GUI和数据库的注册和登录程序示例:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class RegisterAndLogin extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField tfId, tfUsername, tfPassword;
private JButton btnRegister, btnLogin;
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public RegisterAndLogin() {
super("Register and Login Program");
// 创建GUI组件
JLabel lbId = new JLabel("ID:");
JLabel lbUsername = new JLabel("Username:");
JLabel lbPassword = new JLabel("Password:");
tfId = new JTextField(10);
tfUsername = new JTextField(10);
tfPassword = new JPasswordField(10);
btnRegister = new JButton("Register");
btnLogin = new JButton("Login");
JPanel p1 = new JPanel(new GridLayout(3, 2));
p1.add(lbId);
p1.add(tfId);
p1.add(lbUsername);
p1.add(tfUsername);
p1.add(lbPassword);
p1.add(tfPassword);
JPanel p2 = new JPanel(new FlowLayout());
p2.add(btnRegister);
p2.add(btnLogin);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(p1, BorderLayout.CENTER);
contentPane.add(p2, BorderLayout.SOUTH);
// 注册事件监听器
btnRegister.addActionListener(this);
btnLogin.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 设置窗口属性
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRegister) {
String id = tfId.getText().trim();
String username = tfUsername.getText().trim();
String password = tfPassword.getText().trim();
if (id.equals("") || username.equals("") || password.equals("")) {
JOptionPane.showMessageDialog(null, "Please fill in all fields.");
return;
}
try {
rs = stmt.executeQuery("select * from users where id='" + id + "'");
if (rs.next()) {
JOptionPane.showMessageDialog(null, "ID already exists.");
} else {
stmt.executeUpdate("insert into users(id,username,password) values('" + id + "','" + username + "','" + password + "')");
JOptionPane.showMessageDialog(null, "Registration successful.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == btnLogin) {
String id = tfId.getText().trim();
String password = tfPassword.getText().trim();
if (id.equals("") || password.equals("")) {
JOptionPane.showMessageDialog(null, "Please fill in all fields.");
return;
}
try {
rs = stmt.executeQuery("select * from users where id='" + id + "' and password='" + password + "'");
if (rs.next()) {
JOptionPane.showMessageDialog(null, "Login successful.");
} else {
JOptionPane.showMessageDialog(null, "Login failed, please try again.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new RegisterAndLogin();
}
}
```
这个程序使用了MySQL数据库,并在`JDBC:mysql://localhost:3306/test`中连接到了名为`test`的数据库,用户名为`root`,密码为`123456`。你需要根据自己的实际情况修改这些参数。
当用户点击“Register”按钮时,程序将检查输入的ID是否已经存在于数据库中。如果是,将弹出一个信息框提示“ID already exists.”;否则,将向数据库中插入一条新的记录,并弹出一个信息框提示“Registration successful.”。
当用户点击“Login”按钮时,程序将检查输入的ID和密码是否与数据库中的记录匹配。如果是,将弹出一个信息框提示“Login successful.”;否则,将弹出一个信息框提示“Login failed, please try again.”。
在程序运行结束后,应该调用`conn.close()`关闭数据库连接。