java GUI和MySQL编程程序实现模拟某学籍系统的注册和登录功能:注册时需要检查其输入的12位学号是否全是数字(前4位是2005-2022范围内的年份)且不和已注册的学号重复,密码是6-12位的字符,【选做功能:可以采用随机生成的图片作为为验证码】,注册成功则显示成功提示,否则出现相应的错误提醒;登录时根据具体登录情况显示成功或相应的错误信息。要求该程序采用GUI界面,且将学生的学号及登录密码保存在一个数据表中,实验内容应展示数据库及数据表的设计等相关信息的代码
时间: 2024-03-02 07:49:03 浏览: 51
学生信息管理系统JAVA GUI +MYSQL
5星 · 资源好评率100%
以下是Java代码示例,实现模拟某学籍系统的注册和登录功能:
```
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Random;
import javax.swing.*;
public class StudentSystem extends JFrame {
private JTextField idField;
private JPasswordField passwordField;
private JTextField captchaField;
private JLabel captchaLabel;
private JButton registerButton;
private JButton loginButton;
private JLabel statusLabel;
private Connection conn;
public StudentSystem() {
// 初始化界面
setTitle("学籍系统");
setSize(400, 300);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 2));
// 添加学号输入框
add(new JLabel("学号:"));
idField = new JTextField(12);
add(idField);
// 添加密码输入框
add(new JLabel("密码:"));
passwordField = new JPasswordField(12);
add(passwordField);
// 添加验证码输入框
add(new JLabel("验证码:"));
captchaField = new JTextField(4);
add(captchaField);
// 添加验证码图片
captchaLabel = new JLabel();
updateCaptcha();
add(captchaLabel);
// 添加注册按钮
registerButton = new JButton("注册");
registerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
register();
}
});
add(registerButton);
// 添加登录按钮
loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
login();
}
});
add(loginButton);
// 添加状态信息标签
statusLabel = new JLabel("欢迎使用学籍系统", JLabel.CENTER);
add(statusLabel);
// 连接MySQL数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
System.out.println("数据库连接成功");
} catch (Exception e) {
System.out.println("数据库连接失败:" + e.getMessage());
}
setVisible(true);
}
// 更新验证码
private void updateCaptcha() {
Random rand = new Random();
String captcha = "";
for (int i = 0; i < 4; i++) {
captcha += rand.nextInt(10);
}
captchaLabel.setText(captcha);
}
// 注册功能
private void register() {
String id = idField.getText();
String password = new String(passwordField.getPassword());
String captcha = captchaField.getText();
if (!id.matches("\\d{12}") || Integer.parseInt(id.substring(0, 4)) < 2005 || Integer.parseInt(id.substring(0, 4)) > 2022) {
JOptionPane.showMessageDialog(this, "学号格式不正确", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (password.length() < 6 || password.length() > 12) {
JOptionPane.showMessageDialog(this, "密码长度必须为6-12位", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (!captcha.equals(captchaLabel.getText())) {
JOptionPane.showMessageDialog(this, "验证码错误", "错误", JOptionPane.ERROR_MESSAGE);
updateCaptcha();
return;
}
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students WHERE id='" + id + "'");
if (rs.next()) {
JOptionPane.showMessageDialog(this, "学号已被注册", "错误", JOptionPane.ERROR_MESSAGE);
} else {
stmt.executeUpdate("INSERT INTO students VALUES('" + id + "','" + password + "')");
JOptionPane.showMessageDialog(this, "注册成功", "成功", JOptionPane.INFORMATION_MESSAGE);
}
} catch (Exception e) {
System.out.println("数据库操作失败:" + e.getMessage());
}
}
// 登录功能
private void login() {
String id = idField.getText();
String password = new String(passwordField.getPassword());
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students WHERE id='" + id + "' AND password='" + password + "'");
if (rs.next()) {
statusLabel.setText("登录成功");
} else {
JOptionPane.showMessageDialog(this, "学号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
System.out.println("数据库操作失败:" + e.getMessage());
}
}
public static void main(String[] args) {
new StudentSystem();
}
}
```
以上代码实现了一个简单的学籍系统,包括注册和登录功能。需要注意的是,由于安全性问题,实际使用中需要对密码进行加密存储。
阅读全文