This Java and MySQL project implements a student exam portal with features for both student and teacher accounts.

Student Account Features:

  • Registration: Students can register for the system with their name, student ID, and password.
  • Login: Students can log in using their student ID and password.
  • Exam Taking: Students can select a course and start an exam, consisting of 10 multiple-choice questions (MCQ) with four options each.
  • Score Tracking: After completing the exam, the student's score is displayed and saved to the database.

Teacher Account Features:

  • Question Management: Teachers can view, add, delete, and update exam questions in the system.

Code Example:

Student Account:

import java.sql.*;
import java.util.Scanner;

public class StudentPortal {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/exam_portal", "root", "");

            System.out.println("Welcome to Exam Portal!");
            System.out.println("1. Register");
            System.out.println("2. Login");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();

            if (choice == 1) {
                System.out.print("Enter your name: ");
                String name = scanner.next();
                System.out.print("Enter your student ID: ");
                String id = scanner.next();
                System.out.print("Enter your password: ");
                String password = scanner.next();

                stmt = conn.createStatement();
                String sql = "INSERT INTO students (name, student_id, password) VALUES ('" + name + "', '" + id + "', '" + password + "')";
                stmt.executeUpdate(sql);

                System.out.println("Registration successful!");
            } else if (choice == 2) {
                System.out.print("Enter your student ID: ");
                String id = scanner.next();
                System.out.print("Enter your password: ");
                String password = scanner.next();

                stmt = conn.createStatement();
                String sql = "SELECT * FROM students WHERE student_id = '" + id + "' AND password = '" + password + "'";
                rs = stmt.executeQuery(sql);

                if (rs.next()) {
                    System.out.println("Login successful!");
                    System.out.println("1. Start exam");
                    System.out.println("2. Logout");
                    System.out.print("Enter your choice: ");
                    int choice2 = scanner.nextInt();

                    if (choice2 == 1) {
                        System.out.println("Select course:");
                        System.out.println("1. Math");
                        System.out.println("2. Science");
                        System.out.print("Enter your choice: ");
                        int course = scanner.nextInt();

                        int score = 0;
                        for (int i = 1; i <= 10; i++) {
                            System.out.println("Question " + i + ":");
                            System.out.println("What is the capital of France?");
                            System.out.println("A. Paris");
                            System.out.println("B. London");
                            System.out.println("C. Berlin");
                            System.out.println("D. Rome");
                            System.out.print("Enter your answer (A/B/C/D): ");
                            String answer = scanner.next();

                            if (answer.equals("A")) {
                                score += 10;
                                System.out.println("Correct!");
                            } else {
                                System.out.println("Incorrect!");
                            }
                        }

                        System.out.println("Your final score is " + score + "/100.");
                        sql = "INSERT INTO scores (student_id, course, score) VALUES ('" + id + "', " + course + ", " + score + ")";
                        stmt.executeUpdate(sql);
                    } else if (choice2 == 2) {
                        System.out.println("Logout successful!");
                    }
                } else {
                    System.out.println("Invalid student ID or password!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { rs.close(); } catch (Exception e) { }
            try { stmt.close(); } catch (Exception e) { }
            try { conn.close(); } catch (Exception e) { }
        }
    }

}

**Teacher Account:**

```java
import java.sql.*;
import java.util.Scanner;

public class TeacherPortal {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/exam_portal", "root", "");

            System.out.println("Welcome to Exam Portal (Teacher Mode)!");
            System.out.println("1. View questions");
            System.out.println("2. Add question");
            System.out.println("3. Delete question");
            System.out.println("4. Update question");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();

            if (choice == 1) {
                stmt = conn.createStatement();
                String sql = "SELECT * FROM questions";
                rs = stmt.executeQuery(sql);

                System.out.println("Questions:");
                while (rs.next()) {
                    System.out.println(rs.getString("question"));
                }
            } else if (choice == 2) {
                System.out.print("Enter the question: ");
                String question = scanner.next();
                System.out.print("Enter option A: ");
                String optionA = scanner.next();
                System.out.print("Enter option B: ");
                String optionB = scanner.next();
                System.out.print("Enter option C: ");
                String optionC = scanner.next();
                System.out.print("Enter option D: ");
                String optionD = scanner.next();
                System.out.print("Enter the correct answer (A/B/C/D): ");
                String answer = scanner.next();

                stmt = conn.createStatement();
                String sql = "INSERT INTO questions (question, option_a, option_b, option_c, option_d, answer) VALUES ('" + question + "', '" + optionA + "', '" + optionB + "', '" + optionC + "', '" + optionD + "', '" + answer + "')";
                stmt.executeUpdate(sql);

                System.out.println("Question added successfully!");
            } else if (choice == 3) {
                System.out.print("Enter the question ID to delete: ");
                int id = scanner.nextInt();

                stmt = conn.createStatement();
                String sql = "DELETE FROM questions WHERE id = " + id;
                stmt.executeUpdate(sql);

                System.out.println("Question deleted successfully!");
            } else if (choice == 4) {
                System.out.print("Enter the question ID to update: ");
                int id = scanner.nextInt();
                System.out.print("Enter the new question: ");
                String question = scanner.next();
                System.out.print("Enter new option A: ");
                String optionA = scanner.next();
                System.out.print("Enter new option B: ");
                String optionB = scanner.next();
                System.out.print("Enter new option C: ");
                String optionC = scanner.next();
                System.out.print("Enter new option D: ");
                String optionD = scanner.next();
                System.out.print("Enter the new correct answer (A/B/C/D): ");
                String answer = scanner.next();

                stmt = conn.createStatement();
                String sql = "UPDATE questions SET question = '" + question + "', option_a = '" + optionA + "', option_b = '" + optionB + "', option_c = '" + optionC + "', option_d = '" + optionD + "', answer = '" + answer + "' WHERE id = " + id;
                stmt.executeUpdate(sql);

                System.out.println("Question updated successfully!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { rs.close(); } catch (Exception e) { }
            try { stmt.close(); } catch (Exception e) { }
            try { conn.close(); } catch (Exception e) { }
        }
    }

}

This is a simplified example; in a real-world application, you'd need to implement more robust error handling, input validation, and user interface design.

Java & MySQL Exam Portal: Student & Teacher Management System