您现在的位置是:首页 > 技术教程 正文

java实验:数据库应用(idea+mysql+php)设计用户注册和登录

admin 阅读: 2024-03-15
后台-插件-广告管理-内容页头部广告(手机)

设计用户注册和登录界面,实现用户注册和登录操作。

  1. 设计用户注册/登录界面;
  2. 使用工具在MySQL中创建user表,包括学号、姓名、密码、专业、班级;
  3. 实现注册操作:在user表中插入一条新纪录,但学号不能重复;
  4. 实现登录操作:使用输入的学号和密码在user表中查找,查找成功,弹出消息框显示“登录成功”,不成功,则回到输入界面;使用对象文件记住在本机登录的用户名,显示在用户名组合框中。

因为我是使用的php和Navicat for MySQL创建数据库(电脑上最好不要下载两个版本的mysql,否则会出现问题),而如果使用MySQL Installer for Windows来进行数据库设计,大家可以点击下面这个链接,这个博主大大写的超详细。(个人觉得Navicat for MySQL更方便,数据操作比较简单)。如果说之前没有安装过phpstudy,那就最好跟着下面这个博主尝试一下。

在Windows上配置MySql开发java,导入JDBC的jar包后连接SQL Server数据库&结合Java和MySql的一些简单实践-CSDN博客

一、首先去官网下载mysql和phpstudy。

这两个建议一起下,接着是配置环境,可以搜一下配置教程。

启动如上图所示的mysql和apache服务。

点击创建数据库。

根据提示填写,我这里的数据库名称是student,用户名ly,密码123456,最后点击确认。

接着转到mysql,

找到连接,点击mysql,会有一下窗户:

就依次按刚创建的数据库填写连接名,用户名,密码,其他不用改动,点击确定。

、打开创建好的连接,新建表

点击保存。

继续保存,ctrl+s。

、代码部分

代码是在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就可以了。

然后新建类粘贴一下代码:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.*;
  6. public class UserRegistrationLogin extends JFrame {
  7. private JTextField studentIdField;
  8. private JPasswordField passwordField;
  9. private JTextField nameField;
  10. private JTextField majorField;
  11. private JTextField classField;
  12. private JComboBox usernameComboBox;
  13. private Connection conn;
  14. public UserRegistrationLogin() {
  15. super("User Registration and Login");
  16. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  17. this.setSize(400, 300);
  18. this.setLocationRelativeTo(null);
  19. JPanel panel = new JPanel(new GridLayout(6, 2));
  20. JLabel studentIdLabel = new JLabel("Student ID:");
  21. studentIdField = new JTextField();
  22. JLabel passwordLabel = new JLabel("Password:");
  23. passwordField = new JPasswordField();
  24. JLabel nameLabel = new JLabel("Name:");
  25. nameField = new JTextField();
  26. JLabel majorLabel = new JLabel("Major:");
  27. majorField = new JTextField();
  28. JLabel classLabel = new JLabel("Class:");
  29. classField = new JTextField();
  30. JLabel usernameLabel = new JLabel("Username:");
  31. usernameComboBox = new JComboBox<>();
  32. JButton registerButton = new JButton("Register");
  33. JButton loginButton = new JButton("Login");
  34. panel.add(studentIdLabel);
  35. panel.add(studentIdField);
  36. panel.add(passwordLabel);
  37. panel.add(passwordField);
  38. panel.add(nameLabel);
  39. panel.add(nameField);
  40. panel.add(majorLabel);
  41. panel.add(majorField);
  42. panel.add(classLabel);
  43. panel.add(classField);
  44. panel.add(usernameLabel);
  45. panel.add(usernameComboBox);
  46. panel.add(registerButton);
  47. panel.add(loginButton);
  48. this.getContentPane().add(panel);
  49. registerButton.addActionListener(new ActionListener() {
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. registerUser();
  53. }
  54. });
  55. loginButton.addActionListener(new ActionListener() {
  56. @Override
  57. public void actionPerformed(ActionEvent e) {
  58. loginUser();
  59. }
  60. });
  61. try {
  62. String driver = "com.mysql.cj.jdbc.Driver";
  63. String url = "jdbc:mysql://localhost/student?user=ly&password=123456";
  64. Class.forName(driver);
  65. conn = DriverManager.getConnection(url);
  66. loadUsernames();
  67. } catch (ClassNotFoundException | SQLException ex) {
  68. ex.printStackTrace();
  69. }
  70. }
  71. private void registerUser() {
  72. String studentId = studentIdField.getText();
  73. String password = new String(passwordField.getPassword());
  74. String name = nameField.getText();
  75. String major = majorField.getText();
  76. String className = classField.getText();
  77. if (studentId.isEmpty() || password.isEmpty() || name.isEmpty() || major.isEmpty() || className.isEmpty()) {
  78. JOptionPane.showMessageDialog(this, "Please fill in all fields");
  79. return;
  80. }
  81. try {
  82. PreparedStatement stmt = conn.prepareStatement("INSERT INTO student (student_id, name, password, major, class) VALUES (?, ?, ?, ?, ?)");
  83. stmt.setString(1, studentId);
  84. stmt.setString(2, name);
  85. stmt.setString(3, password);
  86. stmt.setString(4, major);
  87. stmt.setString(5, className);
  88. stmt.executeUpdate();
  89. JOptionPane.showMessageDialog(this, "Registration successful");
  90. studentIdField.setText("");
  91. passwordField.setText("");
  92. nameField.setText("");
  93. majorField.setText("");
  94. classField.setText("");
  95. loadUsernames();
  96. } catch (SQLException ex) {
  97. JOptionPane.showMessageDialog(this, "Registration failed: " + ex.getMessage());
  98. }
  99. }
  100. private void loginUser() {
  101. String studentId = studentIdField.getText();
  102. String password = new String(passwordField.getPassword());
  103. try {
  104. PreparedStatement stmt = conn.prepareStatement("SELECT * FROM student WHERE student_id = ? AND password = ?");
  105. stmt.setString(1, studentId);
  106. stmt.setString(2, password);
  107. ResultSet rs = stmt.executeQuery();
  108. if (rs.next()) {
  109. String name = rs.getString("name");
  110. JOptionPane.showMessageDialog(this, "Login successful. Welcome, " + name + "!");
  111. // Remember the username for future logins
  112. String selectedUsername = (String) usernameComboBox.getSelectedItem();
  113. if (selectedUsername == null || !selectedUsername.equals(name)) {
  114. usernameComboBox.addItem(name);
  115. usernameComboBox.setSelectedItem(name);
  116. }
  117. studentIdField.setText("");
  118. passwordField.setText("");
  119. } else {
  120. JOptionPane.showMessageDialog(this, "Login failed. Please check your student ID and password.");
  121. }
  122. } catch (SQLException ex) {
  123. JOptionPane.showMessageDialog(this, "Login failed: " + ex.getMessage());
  124. }
  125. }
  126. private void loadUsernames() {
  127. try {
  128. Statement stmt = conn.createStatement();
  129. ResultSet rs = stmt.executeQuery("SELECT DISTINCT name FROM student");
  130. usernameComboBox.removeAllItems();
  131. while (rs.next()) {
  132. String name = rs.getString("name");
  133. usernameComboBox.addItem(name);
  134. }
  135. } catch (SQLException ex) {
  136. ex.printStackTrace();
  137. }
  138. }
  139. public static void main(String[] args) {
  140. SwingUtilities.invokeLater(new Runnable() {
  141. @Override
  142. public void run() {
  143. UserRegistrationLogin frame = new UserRegistrationLogin();
  144. frame.setVisible(true);
  145. }
  146. });
  147. }
  148. }

效果展示:

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索