将口算题生成软件中的题目及习题保存到MySQL数据库中,并实现题目的保存和读取。实现效果截图及相关代码。






package SQL;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainMenu extends JFrame {
private static final long serialVersionUID = 1L; //定义程序序列化,1L是默认定义
protected static String s1;
protected static String s2;
protected static String s3;
private JLabel jl1 = new JLabel("欢迎来到数学答题系统");
private JLabel jl3 = new JLabel(" ");
private JLabel jl4 = new JLabel(" ");
private JButton JB1 = new JButton("开始答题");
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private JPanel jp3 = new JPanel();
private JPanel jp4 = new JPanel();
private JPanel jp5 = new JPanel();
private JPanel jp6 = new JPanel();
private JPanel jp7 = new JPanel();
private void Event() {
JB1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
setVisible(false);
dispose();
new Test();
}
});
}
public MainMenu() {
this.setTitle("欢迎来到口算答题系统");
this.setSize(500, 450);
this.setLocationRelativeTo(null); //将此窗口置于屏幕的中央
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout(20,20));
jl1.setFont(new Font("黑体",Font.BOLD,35));
jl1.setForeground(Color.green);
jp1.add(jl1);
jp1.setLayout(new GridBagLayout());
this.add(jp1,BorderLayout.NORTH);
jp7.add(JB1);
this.add(jp7,BorderLayout.SOUTH);
//美化界面
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Event();
this.setVisible(true);
}
public static void main(String[] args) {
new MainMenu();
}
}
package SQL;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class Test extends JFrame {
private int max = 50;
private int count = 1; // Added declaration for count
private int a, b; // Added declaration for a and b
private JLabel JLgross = new JLabel("共10页");
private JLabel JLpage = new JLabel();
private JLabel jl1 = new JLabel(" ");
private JLabel JLhour = new JLabel();
private JLabel JLminute = new JLabel();
private JLabel JLseconds = new JLabel();
private JButton JB1 = new JButton("提交");
private JButton b1 = new JButton("首页");
private JButton b2 = new JButton("上一页");
private JButton b3 = new JButton("下一页");
private JButton b4 = new JButton("尾页");
private JButton b5 = new JButton("批改答案");
private JButton b6 = new JButton("返回");
private JLabel jl[] = new JLabel[50];
private JTextField jtf[] = new JTextField[50];
private JTextField jta[] = new JTextField[50];
private JPanel pnl1 = new JPanel();
private JPanel pnl2 = new JPanel();
private JPanel pnl3 = new JPanel();
private JPanel pnl4 = new JPanel();
static CardLayout care = new CardLayout();
int[] answer = new int[max];
String[] studentAnswer = new String[max];
public long time = 0;
// 数据库连接参数
private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private static final String CREATE_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS math (id INT AUTO_INCREMENT PRIMARY KEY, question TEXT, answer INT)";
private static final String INSERT_QUESTION_QUERY = "INSERT INTO math (question, answer) VALUES (?, ?)";
private void saveToDatabase() {
try (Connection connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
Statement statement = connection.createStatement()) {
// 如果表不存在,创建 'math' 表
statement.executeUpdate(CREATE_TABLE_QUERY);
// 将问题和答案插入数据库
try (PreparedStatement preparedStatement = connection.prepareStatement(INSERT_QUESTION_QUERY)) {
for (int i = 0, k = 0; i < max; i++, k++) {
preparedStatement.setString(1, jl[k].getText()); // 插入问题
preparedStatement.setInt(2, answer[k]); // 插入答案
preparedStatement.executeUpdate();
}
}
JOptionPane.showMessageDialog(null, "问题已保存到数据库。", "成功", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "保存问题到数据库时发生错误。", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void Event() {
JB1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int correctAnswer = 0;
for (int i = 0, k = 0; i < max; i++, k++) {
studentAnswer[i] = jtf[k].getText().trim();
try {
if (Integer.parseInt(studentAnswer[i]) == answer[k]) {
correctAnswer++;
}
} catch (NumberFormatException u) {
// 处理转换异常
}
}
int score = 100 * correctAnswer / max;
String s = "共50道题\n";
s = s + "答对" + correctAnswer + "道题\n";
s = s + "答错" + (max - correctAnswer) + "道题\n";
s = s + "成绩" + String.format("%d", score) + "分\n";
s = s + "正确率:" + correctAnswer * 100 / 50 + "%\n";
s = s + "答题时间:" + time + "秒";
JOptionPane.showMessageDialog(null, s, "本次答题情况", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "即将返回首页面", "提示", JOptionPane.ERROR_MESSAGE);
saveToDatabase(); // 保存问题到数据库
new MainMenu();
setVisible(false);
dispose();
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
care.first(pnl3);
count = 1;
JLpage.setText("第" + count + "页");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
care.previous(pnl3);
if (count > 1 && count <= 10) {
count--;
} else {
count = 10;
}
JLpage.setText("第" + count + "页");
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
care.next(pnl3);
if (count >= 1 && count < 10) {
count++;
} else {
count = 1;
}
JLpage.setText("第" + count + "页");
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
care.last(pnl3);
count = 10;
JLpage.setText("第" + count + "页");
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0, k = 0; i < max; i++, k++) {
studentAnswer[i] = jtf[k].getText().trim();
try {
if (Integer.parseInt(studentAnswer[i]) == answer[k]) {
jta[k].setText("回答正确!!");
jta[k].setForeground(Color.GREEN);
} else {
jta[k].setText("回答错误!! 正确答案为:" + jl[k].getText() + answer[k]);
jta[k].setForeground(Color.RED);
}
} catch (Exception u) {
// 处理转换异常
}
}
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "是否返回首页", "提示", JOptionPane.ERROR_MESSAGE);
new MainMenu();
setVisible(false);
dispose();
}
});
}
public Test() {
this.setTitle("开始答题");
this.setSize(830, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout(20, 20));
pnl1.add(JLhour);
pnl1.add(JLminute);
pnl1.add(JLseconds);
JLpage.setText("第" + count + "页");
pnl2.setLayout(new FlowLayout());
pnl2.add(JLgross);
pnl2.add(JLpage);
pnl2.add(jl1);
pnl2.add(pnl1);
pnl2.add(JB1);
this.add(pnl2, BorderLayout.NORTH);
pnl3.setLayout(care);
this.add(pnl3, BorderLayout.CENTER);
pnl3.setBorder(BorderFactory.createLoweredBevelBorder());
for (int i = 0, k = 0; i < 10; i++) {
JPanel p = new JPanel();
p.setLayout(new GridLayout(5, 2, 20, 20));
for (int j = 0; j < 5; j++) {
a = (int) (Math.random() * 100 + 1);
b = (int) (Math.random() * 100 + 1);
String random = "";
String[] doc = {"+", "-"};
int index = (int) (Math.random() * doc.length);
random = doc[index];
if (random.equals("-")) {
if (a > b) {
jl[k] = new JLabel(a + random + b + "=");
jl[k].setFont(new Font("黑体", Font.BOLD, 20));
p.add(jl[k]);
answer[k] = a - b;
} else {
jl[k] = new JLabel(b + random + a + "=");
jl[k].setFont(new Font("黑体", Font.BOLD, 20));
p.add(jl[k]);
answer[k] = b - a;
}
} else if (random.equals("+")) {
jl[k] = new JLabel(a + random + b + "=");
jl[k].setFont(new Font("黑体", Font.BOLD, 20));
p.add(jl[k]);
answer[k] = a + b;
}
jtf[k] = new JTextField(6);
jta[k] = new JTextField(6);
p.add(jtf[k]);
p.add(jta[k]);
jtf[k].setText(null);
jtf[k].setText(null);
k++;
}
pnl3.add(p);
}
pnl4.setLayout(new GridLayout(1, 5));
pnl4.add(b1);
pnl4.add(b2);
pnl4.add(b3);
pnl4.add(b4);
pnl4.add(b5);
pnl4.add(b6);
this.add(pnl4, BorderLayout.SOUTH);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Event();
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}