用java的gui和jdbc实现学生成绩管理,登陆页面实现注册和登陆,操作页面实现学生成绩的增删查改,并给出具体代码

时间: 2024-03-10 14:49:00 浏览: 21
好的,下面是一个简单的示例,实现了学生成绩管理系统的登陆页面、操作页面、数据库的设计和实现。 1. 登陆页面的代码如下: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; public class LoginFrame extends JFrame implements ActionListener { private JLabel labelUser; private JLabel labelPwd; private JTextField fieldUser; private JPasswordField fieldPwd; private JButton buttonLogin; private JButton buttonRegister; public LoginFrame() { super("Login"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); labelUser = new JLabel("Username:"); labelPwd = new JLabel("Password:"); fieldUser = new JTextField(20); fieldPwd = new JPasswordField(20); buttonLogin = new JButton("Login"); buttonRegister = new JButton("Register"); JPanel panel = new JPanel(new GridLayout(3, 2)); panel.add(labelUser); panel.add(fieldUser); panel.add(labelPwd); panel.add(fieldPwd); panel.add(buttonLogin); panel.add(buttonRegister); add(panel); buttonLogin.addActionListener(this); buttonRegister.addActionListener(this); pack(); setLocationRelativeTo(null); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonLogin) { String user = fieldUser.getText(); String pwd = new String(fieldPwd.getPassword()); if (login(user, pwd)) { dispose(); new MainFrame(); } else { JOptionPane.showMessageDialog(null, "Invalid username or password."); } } else if (e.getSource() == buttonRegister) { String user = fieldUser.getText(); String pwd = new String(fieldPwd.getPassword()); if (register(user, pwd)) { JOptionPane.showMessageDialog(null, "Register successfully."); } else { JOptionPane.showMessageDialog(null, "Failed to register."); } } } private boolean login(String user, String pwd) { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE username = ? AND password = ?"); ps.setString(1, user); ps.setString(2, pwd); ResultSet rs = ps.executeQuery(); return rs.next(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); return false; } } private boolean register(String user, String pwd) { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); PreparedStatement ps = conn.prepareStatement("INSERT INTO user (username, password) VALUES (?, ?)"); ps.setString(1, user); ps.setString(2, pwd); int result = ps.executeUpdate(); return result > 0; } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); return false; } } public static void main(String[] args) { new LoginFrame(); } } ``` 2. 操作页面的代码如下: ```java import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; public class MainFrame extends JFrame implements ActionListener { private JButton buttonAdd; private JButton buttonDelete; private JButton buttonQuery; private JButton buttonUpdate; private JTable table; private DefaultTableModel model; public MainFrame() { super("Student Score Management"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonAdd = new JButton("Add"); buttonDelete = new JButton("Delete"); buttonQuery = new JButton("Query"); buttonUpdate = new JButton("Update"); JPanel panelButtons = new JPanel(new FlowLayout()); panelButtons.add(buttonAdd); panelButtons.add(buttonDelete); panelButtons.add(buttonQuery); panelButtons.add(buttonUpdate); add(panelButtons, BorderLayout.NORTH); model = new DefaultTableModel(new Object[][]{}, new String[]{"ID", "Name", "Score"}); table = new JTable(model); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane, BorderLayout.CENTER); buttonAdd.addActionListener(this); buttonDelete.addActionListener(this); buttonQuery.addActionListener(this); buttonUpdate.addActionListener(this); pack(); setLocationRelativeTo(null); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonAdd) { addStudent(); } else if (e.getSource() == buttonDelete) { deleteStudent(); } else if (e.getSource() == buttonQuery) { queryStudents(); } else if (e.getSource() == buttonUpdate) { updateStudent(); } } private void addStudent() { String name = JOptionPane.showInputDialog("Input name:"); String score = JOptionPane.showInputDialog("Input score:"); try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); PreparedStatement ps = conn.prepareStatement("INSERT INTO student (name, score) VALUES (?, ?)"); ps.setString(1, name); ps.setString(2, score); int result = ps.executeUpdate(); if (result > 0) { JOptionPane.showMessageDialog(null, "Add successfully."); } else { JOptionPane.showMessageDialog(null, "Failed to add."); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Failed to add."); } } private void deleteStudent() { int row = table.getSelectedRow(); if (row < 0) { JOptionPane.showMessageDialog(null, "Please select a row to delete."); return; } int id = (int) model.getValueAt(row, 0); try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id = ?"); ps.setInt(1, id); int result = ps.executeUpdate(); if (result > 0) { JOptionPane.showMessageDialog(null, "Delete successfully."); } else { JOptionPane.showMessageDialog(null, "Failed to delete."); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Failed to delete."); } } private void queryStudents() { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); PreparedStatement ps = conn.prepareStatement("SELECT * FROM student"); ResultSet rs = ps.executeQuery(); model.setRowCount(0); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String score = rs.getString("score"); model.addRow(new Object[]{id, name, score}); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } private void updateStudent() { int row = table.getSelectedRow(); if (row < 0) { JOptionPane.showMessageDialog(null, "Please select a row to update."); return; } int id = (int) model.getValueAt(row, 0); String name = JOptionPane.showInputDialog("Input name:"); String score = JOptionPane.showInputDialog("Input score:"); try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); PreparedStatement ps = conn.prepareStatement("UPDATE student SET name = ?, score = ? WHERE id = ?"); ps.setString(1, name); ps.setString(2, score); ps.setInt(3, id); int result = ps.executeUpdate(); if (result > 0) { JOptionPane.showMessageDialog(null, "Update successfully."); } else { JOptionPane.showMessageDialog(null, "Failed to update."); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Failed to update."); } } public static void main(String[] args) { new MainFrame(); } } ``` 3. 数据库的设计和实现 学生信息表的设计如下: ``` CREATE TABLE student ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), score INT ); ``` 用户信息表的设计如下: ``` CREATE TABLE user ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20), password VARCHAR(20) ); ``` 需要注意的是,上述代码中的数据库连接信息需要根据实际情况进行修改。

相关推荐

最新推荐

recommend-type

JDBC+GUI实现简单学生管理系统

主要为大家详细介绍了JDBC+GUI实现简单学生管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Python实现GUI学生信息管理系统

主要介绍了Python实现GUI学生信息管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

java GUI实现五子棋游戏

主要为大家详细介绍了java GUI实现五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

人工智能导论大学生期末复习测试题

人工智能导论大学生期末复习测试题
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依