本文介绍: 代码是在idea中编写,首先要下载驱动文件mysql–connector-j-8.2.0.jar,根据提示填写,我这里的数据库名称是student,用户名ly,密码123456,最后点击确认。找到刚刚导入的mysql–connector-j-8.2.0.jar,双击点ok就可以了。下载之后,把mysql–connector-j-8.2.0.jar复制到项目中。就依次按刚创建的数据库填写连接名,用户名,密码,其他不用改动,点击确定。这两个建议一起下,接着是配置环境,可以搜一下配置教程。接着转到mysql,
- 设计用户注册/登录界面;
- 使用工具在MySQL中创建user表,包括学号、姓名、密码、专业、班级;
- 实现注册操作:在user表中插入一条新纪录,但学号不能重复;
- 实现登录操作:使用输入的学号和密码在user表中查找,查找成功,弹出消息框显示“登录成功”,不成功,则回到输入界面;使用对象文件记住在本机登录的用户名,显示在用户名组合框中。
二、
三、
根据提示填写,我这里的数据库名称是student,用户名ly,密码123456,最后点击确认。
接着转到mysql,
就依次按刚创建的数据库填写连接名,用户名,密码,其他不用改动,点击确定。
点击保存。
代码是在idea中编写,首先要下载驱动文件mysql–connector-j-8.2.0.jar,mysql-connector-java下载_在下是小白的博客-CSDN博客
下载之后,把mysql–connector-j-8.2.0.jar复制到项目中
点击project structure,进入找到modules,点加号选第一个。
找到刚刚导入的mysql–connector-j-8.2.0.jar,双击点ok就可以了。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class UserRegistrationLogin extends JFrame {
private JTextField studentIdField;
private JPasswordField passwordField;
private JTextField nameField;
private JTextField majorField;
private JTextField classField;
private JComboBox<String> usernameComboBox;
private Connection conn;
public UserRegistrationLogin() {
super("User Registration and Login");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 300);
this.setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridLayout(6, 2));
JLabel studentIdLabel = new JLabel("Student ID:");
studentIdField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField();
JLabel majorLabel = new JLabel("Major:");
majorField = new JTextField();
JLabel classLabel = new JLabel("Class:");
classField = new JTextField();
JLabel usernameLabel = new JLabel("Username:");
usernameComboBox = new JComboBox<>();
JButton registerButton = new JButton("Register");
JButton loginButton = new JButton("Login");
panel.add(studentIdLabel);
panel.add(studentIdField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(majorLabel);
panel.add(majorField);
panel.add(classLabel);
panel.add(classField);
panel.add(usernameLabel);
panel.add(usernameComboBox);
panel.add(registerButton);
panel.add(loginButton);
this.getContentPane().add(panel);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
registerUser();
}
});
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginUser();
}
});
try {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost/student?user=ly&password=123456";
Class.forName(driver);
conn = DriverManager.getConnection(url);
loadUsernames();
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
private void registerUser() {
String studentId = studentIdField.getText();
String password = new String(passwordField.getPassword());
String name = nameField.getText();
String major = majorField.getText();
String className = classField.getText();
if (studentId.isEmpty() || password.isEmpty() || name.isEmpty() || major.isEmpty() || className.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields");
return;
}
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO student (student_id, name, password, major, class) VALUES (?, ?, ?, ?, ?)");
stmt.setString(1, studentId);
stmt.setString(2, name);
stmt.setString(3, password);
stmt.setString(4, major);
stmt.setString(5, className);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Registration successful");
studentIdField.setText("");
passwordField.setText("");
nameField.setText("");
majorField.setText("");
classField.setText("");
loadUsernames();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Registration failed: " + ex.getMessage());
}
}
private void loginUser() {
String studentId = studentIdField.getText();
String password = new String(passwordField.getPassword());
try {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM student WHERE student_id = ? AND password = ?");
stmt.setString(1, studentId);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
JOptionPane.showMessageDialog(this, "Login successful. Welcome, " + name + "!");
// Remember the username for future logins
String selectedUsername = (String) usernameComboBox.getSelectedItem();
if (selectedUsername == null || !selectedUsername.equals(name)) {
usernameComboBox.addItem(name);
usernameComboBox.setSelectedItem(name);
}
studentIdField.setText("");
passwordField.setText("");
} else {
JOptionPane.showMessageDialog(this, "Login failed. Please check your student ID and password.");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Login failed: " + ex.getMessage());
}
}
private void loadUsernames() {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT DISTINCT name FROM student");
usernameComboBox.removeAllItems();
while (rs.next()) {
String name = rs.getString("name");
usernameComboBox.addItem(name);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
UserRegistrationLogin frame = new UserRegistrationLogin();
frame.setVisible(true);
}
});
}
}
原文地址:https://blog.csdn.net/m0_75017568/article/details/134750181
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_25748.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。