File 8 of 28: admin/dashboard.jsp

Online Class Attendance System

File 8 of 28: admin/dashboard.jsp

Folder Location: OnlineClassAttendance/admin/dashboard.jsp

1. Purpose of This File

This file retrieves important information for the Admin Dashboard, including the total number of departments, students, teachers, courses, and class sessions.

2. Complete JSP Source Code

<%
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    Integer userId = (Integer) session.getAttribute("userId");
    String role = (String) session.getAttribute("role");

    if (userId == null || !"ADMIN".equals(role)) {

        response.setStatus(401);

        out.print("{\"success\":false,\"message\":\"Unauthorized access\"}");

    } else {

        String dbURL = "jdbc:oracle:thin:@localhost:1521/XEPDB1";
        String dbUser = "YOUR_DB_USERNAME";
        String dbPassword = "YOUR_DB_PASSWORD";

        java.sql.Connection con = null;

        try {

            Class.forName("oracle.jdbc.OracleDriver");

            con = java.sql.DriverManager.getConnection(
                dbURL, dbUser, dbPassword
            );

            String sql =
                "SELECT " +
                "(SELECT COUNT(*) FROM DEPARTMENTS) AS DEPT_COUNT, " +
                "(SELECT COUNT(*) FROM STUDENTS) AS STUDENT_COUNT, " +
                "(SELECT COUNT(*) FROM TEACHERS) AS TEACHER_COUNT, " +
                "(SELECT COUNT(*) FROM COURSES) AS COURSE_COUNT, " +
                "(SELECT COUNT(*) FROM SESSIONS) AS SESSION_COUNT " +
                "FROM DUAL";

            java.sql.PreparedStatement ps =
                con.prepareStatement(sql);

            java.sql.ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                int departments = rs.getInt("DEPT_COUNT");
                int students = rs.getInt("STUDENT_COUNT");
                int teachers = rs.getInt("TEACHER_COUNT");
                int courses = rs.getInt("COURSE_COUNT");
                int sessions = rs.getInt("SESSION_COUNT");

                out.print("{");
                out.print("\"success\":true,");
                out.print("\"departments\":" + departments + ",");
                out.print("\"students\":" + students + ",");
                out.print("\"teachers\":" + teachers + ",");
                out.print("\"courses\":" + courses + ",");
                out.print("\"sessions\":" + sessions);
                out.print("}");

            }

            rs.close();
            ps.close();

        } catch (Exception e) {

            application.log("Dashboard error", e);

            response.setStatus(500);

            out.print("{\"success\":false,\"message\":\"Unable to load dashboard data\"}");

        } finally {

            if (con != null) {
                try {
                    con.close();
                } catch (Exception e) {
                    application.log("Database close error", e);
                }
            }

        }

    }
%>

3. Explanation

  • Session validation: Checks whether the current user is an ADMIN.
  • DriverManager: Establishes the Oracle Database connection.
  • COUNT(*): Counts the records in each database table.
  • DUAL: Oracle's built-in one-row table, used here to return multiple count results.
  • ResultSet: Retrieves the dashboard statistics.
  • JSON response: Sends the statistics to the HTML dashboard.

4. Expected JSON Response

{
    "success": true,
    "departments": 2,
    "students": 150,
    "teachers": 20,
    "courses": 35,
    "sessions": 120
}

Note: These numbers are examples. Actual values will depend on your Oracle Database records.

5. How to Test

Log in as an Admin and open:

http://localhost:8080/OnlineClassAttendance/admin/dashboard.jsp

The browser should display the dashboard statistics in JSON format.

6. Important Database Configuration

Update the database username and password before running the project:

String dbUser = "YOUR_DB_USERNAME";
String dbPassword = "YOUR_DB_PASSWORD";

Next File: File 9 of 28 — admin/departments.html

We will create the Department Management page with a form to add departments and a table to display existing departments.

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন