package 学生成绩查询管理系统.功能; import 学生成绩查询管理系统.界面.Login; import 学生成绩查询管理系统.界面.StudentSystem; 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 AccountManager extends JFrame implements ActionListener { private JMenuItem reLogin; private JMenuItem menu; private JMenuItem exit; private JMenuItem aboutMe; private static final String URL = "jdbc:mysql://localhost:3306/studentmanager"; private static final String USERNAME = "root"; private static final String PASSWORD = "123456"; // String path="C:\\Users\\23513\\Desktop\\学生成绩管理系统\\"; private static Connection connection; private DefaultTableModel tableModel; private JTable dataTable; public AccountManager() { initializeDatabase(); initializeMainFrame(); queryRecords(); initMenu(); } // Initialize database connection private void initializeDatabase() { try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "无法连接到数据库", "错误", JOptionPane.ERROR_MESSAGE); System.exit(1); } } private void initializeMainFrame() { setTitle("学生信息管理界面"); setSize(600, 450); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); // 修改图标 Toolkit tk = Toolkit.getDefaultToolkit(); Image img = tk.getImage("imge\\成绩录入2.png"); setIconImage(img); tableModel = new DefaultTableModel(); tableModel.addColumn("账号"); tableModel.addColumn("密码"); dataTable = new JTable(tableModel); JPanel buttonPanel = new JPanel(); JButton addButton = new JButton("添加数据"); JButton displayButton = new JButton("显示数据"); JButton deleteButton = new JButton("删除数据"); buttonPanel.add(addButton); buttonPanel.add(displayButton); buttonPanel.add(deleteButton); addButton.addActionListener(e -> showAddDataDialog()); displayButton.addActionListener(e -> showDisplayDataDialog()); deleteButton.addActionListener(e -> deleteSelectedRecord()); add(buttonPanel, BorderLayout.NORTH); add(new JScrollPane(dataTable), BorderLayout.CENTER); } private void showAddDataDialog() { AddDataDialog addDataDialog = new AddDataDialog(this, "添加数据", true); addDataDialog.setLocationRelativeTo(this); addDataDialog.setVisible(true); } // Show display data dialog private void showDisplayDataDialog() { queryRecords(); } // Query records from the database private void queryRecords() { try { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM userdatabases"); // Clear the table model tableModel.setRowCount(0); // Populate the table model with data from the database while (resultSet.next()) { Object[] rowData = { resultSet.getString("account"), resultSet.getString("password"), }; tableModel.addRow(rowData); } // Close resources resultSet.close(); statement.close(); } catch (SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "查询数据失败", "错误", JOptionPane.ERROR_MESSAGE); } } // Delete selected record private void deleteSelectedRecord() { int selectedRow = dataTable.getSelectedRow(); if (selectedRow != -1) { try { String accountToDelete = (String) dataTable.getValueAt(selectedRow, 0); String deleteQuery = "DELETE FROM userdatabases WHERE account=?"; try (PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery)) { preparedStatement.setString(1, accountToDelete); preparedStatement.executeUpdate(); } // Update the table model after deletion queryRecords(); JOptionPane.showMessageDialog(this, "记录删除成功", "成功", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "删除记录失败", "错误", JOptionPane.ERROR_MESSAGE); } } else { JOptionPane.showMessageDialog(this, "请选择要删除的记录", "警告", JOptionPane.WARNING_MESSAGE); } } // Add data dialog private static class AddDataDialog extends JDialog { private JTextField accountTextField; private JTextField passwordTextField; public AddDataDialog(JFrame parent, String title, boolean modal) { super(parent, title, modal); setSize(400, 300); // Initialize text fields and button accountTextField = new JTextField(20); passwordTextField = new JTextField(20); JButton addButton = new JButton("添加"); addButton.addActionListener(e -> addRecord()); JPanel panel = new JPanel(new GridLayout(7, 2, 10, 10)); panel.add(new JLabel("账号:")); panel.add(accountTextField); panel.add(new JLabel("密码:")); panel.add(passwordTextField); panel.add(new JLabel()); panel.add(addButton); add(panel); } private void addRecord() { try { String account = accountTextField.getText(); String password = passwordTextField.getText(); String insertQuery = "INSERT INTO userdatabases (account, password) VALUES (?, ?)"; try (PreparedStatement preparedStatement = connection.prepareStatement(insertQuery)) { preparedStatement.setString(1, account); preparedStatement.setString(2, password); preparedStatement.executeUpdate(); } ((AccountManager) getParent()).queryRecords(); JOptionPane.showMessageDialog(this, "记录添加成功", "成功", JOptionPane.INFORMATION_MESSAGE); dispose(); } catch (NumberFormatException | SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "添加记录失败", "错误", JOptionPane.ERROR_MESSAGE); } } } private void initMenu() { JMenuBar jMenuBar = new JMenuBar(); JMenu about = new JMenu("关于我"); JMenu function = new JMenu("功能"); aboutMe = new JMenuItem("QQ二维码"); reLogin = new JMenuItem("重新登录"); menu = new JMenuItem("主页"); exit = new JMenuItem("退出"); about.add(aboutMe); function.add(reLogin); function.add(menu); function.add(exit); aboutMe.addActionListener(this); reLogin.addActionListener(this); menu.addActionListener(this); exit.addActionListener(this); jMenuBar.add(about); jMenuBar.add(function); this.setJMenuBar(jMenuBar); } @Override public void actionPerformed(ActionEvent e) { // 获取当前被点击的条目对象 Object obj = e.getSource(); if (obj == reLogin) { dispose(); new Login(); } else if (obj == menu) { dispose(); new StudentSystem(); } else if (obj == exit) { System.exit(0); } else if (obj == aboutMe) { // 创建一个弹框对象 JDialog jDialog = new JDialog(); Toolkit tk = Toolkit.getDefaultToolkit(); Image img = tk.getImage("imge\\成绩录入2.png"); jDialog.setIconImage(img); // 创建一个管理图片的容器对象JLabel JLabel jLabel = new JLabel(new ImageIcon("imge\\about.png")); // 设置位置和宽高 jLabel.setBounds(0, 0, 300, 380); // 把图片添加到弹框当中 jDialog.getContentPane().add(jLabel); // 给弹框设置大小 jDialog.setSize(400, 400); // 让弹框置顶 jDialog.setAlwaysOnTop(true); // 让弹框居中 jDialog.setLocationRelativeTo(null); // 弹框不关闭则无法操作下面的界面 jDialog.setModal(true); // 让弹框显示出来 jDialog.setVisible(true); } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { AccountManager classroomManager = new AccountManager(); classroomManager.setVisible(true); }); } }