2024/06/12
学习时长:2.5小时
代码行数:121行
博客数量:2篇
完成数据库实验五
2、设计一个小型的数据库应用程序
可利用现有的数据库,也可重新设计数据库。
要求实现数据的增加、删除、修改、查询的功能。
在报告中描述清楚使用的数据库、数据表及实现的功能(要求截图,并附 代码)。
import com.share.Course; import com.share.DatabaseUtil; import com.share.SC; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; public class Main { private DatabaseUtil dbManager = new DatabaseUtil(); private JFrame frame; private JTable courseDataGrid; private JTable scDataGrid; private JButton sortCourseNumberButton; private JButton sortSemesterButton; public Main() { initialize(); } private void initialize() { frame = new JFrame(); frame.setTitle("课程和选课信息"); frame.setSize(1200, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); courseDataGrid = new JTable(); JScrollPane courseScrollPane = new JScrollPane(courseDataGrid); scDataGrid = new JTable(); JScrollPane scScrollPane = new JScrollPane(scDataGrid); sortCourseNumberButton = new JButton("按课程号排序"); sortCourseNumberButton.setPreferredSize(new Dimension(120, 30)); sortSemesterButton = new JButton("按学期排序"); sortSemesterButton.setPreferredSize(new Dimension(120, 30)); sortCourseNumberButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sortDataGrid(0, courseDataGrid); } }); sortSemesterButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sortDataGrid(1, courseDataGrid); } }); refreshCourseDataGrid(); refreshSCDataGrid(); JPanel panel = new JPanel(new GridLayout(2, 1)); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); buttonPanel.add(sortCourseNumberButton); buttonPanel.add(sortSemesterButton); panel.add(courseScrollPane); panel.add(scScrollPane); frame.add(panel, BorderLayout.CENTER); frame.add(buttonPanel, BorderLayout.SOUTH); frame.setVisible(true); } private void refreshCourseDataGrid() { List<Course> courseList = dbManager.getAllCourses(); DefaultTableModel model = new DefaultTableModel(); model.addColumn("课程号"); model.addColumn("学期"); model.addColumn("课程名称"); for (Course course : courseList) { model.addRow(new Object[]{course.getCno(), course.getSemester(), course.getCname()}); } courseDataGrid.setModel(model); } private void refreshSCDataGrid() { List<SC> scList = dbManager.getAllSC(); DefaultTableModel model = new DefaultTableModel(); model.addColumn("选课号"); model.addColumn("学号"); model.addColumn("课程号"); for (SC sc : scList) { model.addRow(new Object[]{sc.getGrade(), sc.getSno(), sc.getCno()}); } scDataGrid.setModel(model); } private void sortDataGrid(int columnIndex, JTable dataGrid) { DefaultTableModel model = (DefaultTableModel) dataGrid.getModel(); model.getDataVector().sort((a, b) -> ((Comparable) a.get(columnIndex)).compareTo(b.get(columnIndex))); model.fireTableDataChanged(); } public static void main(String[] args) { EventQueue.invokeLater(() -> { try { new Main(); } catch (Exception e) { e.printStackTrace(); } }); } }