Create a Java program that generates random math quizzes consisting of 10 questions each. The questions should involve basic arithmetic operations (addition, subtraction, multiplication, and division). After the user completes the quiz, display their score and save it to a local SQLite database.
PLAINFORMAT
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
import java.util.Scanner;

public class MathQuiz {
    private static final String DB_URL = "jdbc:sqlite:math_quiz.db";

    public static void main(String[] args) {
        try {
            // Initialize the database
            initDatabase();

            // Start the quiz
            int score = startQuiz();

            // Save the score to the database
            saveScore(score);

            System.out.println("Your score is: " + score);
        } catch (SQLException e) {
            System.err.println("Error initializing the database:");
            e.printStackTrace();
        }
    }

    private static void initDatabase() throws SQLException {
        try (Connection conn = DriverManager.getConnection(DB_URL)) {
            if (conn != null) {
                String sql = "CREATE TABLE IF NOT EXISTS scores (id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER)";
                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.execute();
            }
        }
    }

    private static int startQuiz() {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int correctAnswers = 0;

        for (int i = 0; i < 10; i++) {
            int num1 = random.nextInt(100);
            int num2 = random.nextInt(100);
            int operation = random.nextInt(4);
            int answer = 0;
            String operationSymbol = "";

            switch (operation) {
                case 0:
                    answer = num1 + num2;
                    operationSymbol = "+";
                    break;
                case 1:
                    answer = num1 - num2;
                    operationSymbol = "-";
                    break;
                case 2:
                    answer = num1 * num2;
                    operationSymbol = "*";
                    break;
                case 3:
                    if (num2 == 0) {
                        i--;
                        continue;
                    }
                    answer = num1 / num2;
                    operationSymbol = "/";
                    break;
            }

            System.out.printf("Question %d: %d %s %d = ", i + 1, num1, operationSymbol, num2);
            int userAnswer = scanner.nextInt();

            if (userAnswer == answer) {
                correctAnswers++;
            }
        }

        return correctAnswers;
    }

    private static void saveScore(int score) throws SQLException {
        try (Connection conn = DriverManager.getConnection(DB_URL)) {
            String sql = "INSERT INTO scores(score) VALUES(?)";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, score);
            stmt.executeUpdate();
        }
    }
}