20231128
请根据今日的课程内容,将口算题生成软件中的题目及习题保存到MySQL数据库中,并实现题目的保存和读取。提交实现效果截图及相关代码。
![](https://img2023.cnblogs.com/blog/2914574/202311/2914574-20231128230815588-840374236.png)
package test; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileWriter; import java.io.IOException; import java.sql.*; import java.util.Random; public class four extends JFrame { private JTextField quantityField, minRangeField, maxRangeField; private JCheckBox additionCheckBox, subtractionCheckBox, multiplicationCheckBox, divisionCheckBox; private JButton generateButton, saveButton; private JTable problemTable; private JButton saveToDatabaseButton; private JButton loadFromDatabaseButton; public four() { setTitle("口算题生成器"); setSize(600, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel leftPanel = new JPanel(); leftPanel.setLayout(new GridLayout(6, 2)); leftPanel.add(new JLabel("出题数量:")); quantityField = new JTextField(); leftPanel.add(quantityField); leftPanel.add(new JLabel("最小值:")); minRangeField = new JTextField(); leftPanel.add(minRangeField); leftPanel.add(new JLabel("最大值:")); maxRangeField = new JTextField(); leftPanel.add(maxRangeField); leftPanel.add(new JLabel("运算符选择:")); JPanel checkBoxPanel = new JPanel(); additionCheckBox = new JCheckBox("加法"); subtractionCheckBox = new JCheckBox("减法"); multiplicationCheckBox = new JCheckBox("乘法"); divisionCheckBox = new JCheckBox("除法"); checkBoxPanel.add(additionCheckBox); checkBoxPanel.add(subtractionCheckBox); checkBoxPanel.add(multiplicationCheckBox); checkBoxPanel.add(divisionCheckBox); leftPanel.add(checkBoxPanel); generateButton = new JButton("生成题目"); generateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { generateProblems(); } }); leftPanel.add(generateButton); saveButton = new JButton("保存题目"); saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { saveProblems(); } }); leftPanel.add(saveButton); // 右侧表格 problemTable = new JTable(); DefaultTableModel model = new DefaultTableModel(); model.addColumn("题目"); problemTable.setModel(model); // 主布局 setLayout(new BorderLayout()); add(leftPanel, BorderLayout.WEST); add(new JScrollPane(problemTable), BorderLayout.CENTER); saveToDatabaseButton = new JButton("保存到数据库"); saveToDatabaseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { saveToDatabase(); } }); leftPanel.add(saveToDatabaseButton); // 新增按钮到左侧面板 loadFromDatabaseButton = new JButton("从数据库加载题目"); loadFromDatabaseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { loadFromDatabase(); } }); leftPanel.add(loadFromDatabaseButton); } private void generateProblems() { DefaultTableModel model = (DefaultTableModel) problemTable.getModel(); model.setRowCount(0); // 清空表格 int quantity = Integer.parseInt(quantityField.getText()); int minRange = Integer.parseInt(minRangeField.getText()); int maxRange = Integer.parseInt(maxRangeField.getText()); Random random = new Random(); for (int i = 0; i < quantity; i++) { int num1 = random.nextInt(maxRange - minRange + 1) + minRange; int num2 = random.nextInt(maxRange - minRange + 1) + minRange; StringBuilder problem = new StringBuilder(); problem.append(num1); if (additionCheckBox.isSelected()) { problem.append(" + ").append(num2); } else if (subtractionCheckBox.isSelected()) { problem.append(" - ").append(num2); } else if (multiplicationCheckBox.isSelected()) { problem.append(" * ").append(num2); } else if (divisionCheckBox.isSelected()) { // Avoid division by zero if (num2 == 0) { continue; } problem.append(" / ").append(num2); } model.addRow(new Object[]{problem.toString()}); } } private void saveProblems() { try { FileWriter writer = new FileWriter("arithmetic_problems.txt"); DefaultTableModel model = (DefaultTableModel) problemTable.getModel(); for (int i = 0; i < model.getRowCount(); i++) { String problem = (String) model.getValueAt(i, 0); writer.write(problem + "\n"); } writer.close(); JOptionPane.showMessageDialog(this, "题目保存成功!", "保存成功", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "保存失败!", "保存失败", JOptionPane.ERROR_MESSAGE); } } private void saveToDatabase() { try { // 建立数据库连接 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/problem", "root", "135790"); // 准备 SQL 语句 String sql = "INSERT INTO problems (problem) VALUES (?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); // 获取表格数据 DefaultTableModel model = (DefaultTableModel) problemTable.getModel(); // 保存每个题目到数据库 for (int i = 0; i < model.getRowCount(); i++) { String problem = (String) model.getValueAt(i, 0); // 设置 SQL 语句参数 preparedStatement.setString(1, problem); // 执行 SQL 语句 preparedStatement.executeUpdate(); } // 关闭连接 preparedStatement.close(); connection.close(); JOptionPane.showMessageDialog(this, "题目保存到数据库成功!", "保存成功", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException exception) { exception.printStackTrace(); JOptionPane.showMessageDialog(this, "保存到数据库失败!", "保存失败", JOptionPane.ERROR_MESSAGE); } } private void loadFromDatabase() { try { // 建立数据库连接 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/problem", "root", "135790"); // 准备 SQL 语句 String sql = "SELECT * FROM problems"; Statement statement = connection.createStatement(); // 执行查询 ResultSet resultSet = statement.executeQuery(sql); // 获取查询结果并更新表格 DefaultTableModel model = (DefaultTableModel) problemTable.getModel(); model.setRowCount(0); // 清空表格 while (resultSet.next()) { String problem = resultSet.getString("problem"); model.addRow(new Object[]{problem}); } // 关闭连接 resultSet.close(); statement.close(); connection.close(); JOptionPane.showMessageDialog(this, "题目从数据库加载成功!", "加载成功", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException exception) { exception.printStackTrace(); JOptionPane.showMessageDialog(this, "从数据库加载题目失败!", "加载失败", JOptionPane.ERROR_MESSAGE); } } public static void main(String[] args) { // 注册数据库驱动程序 try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new four().setVisible(true); } }); } }