-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMSAPP.java
More file actions
138 lines (122 loc) · 5.65 KB
/
LMSAPP.java
File metadata and controls
138 lines (122 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.sql.*;
import java.util.Scanner;
public class LMSApp {
static final String URL = "jdbc:mysql://localhost:3306/dileepdb1";
static final String USER = "root";
static final String PASSWORD = "Dileep@934";
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
Scanner sc = new Scanner(System.in)) {
// Feature 1: Create Tables
createTables(stmt);
int choice;
do {
System.out.println("\n--- Learning Management System ---");
System.out.println("1. Add Student");
System.out.println("2. Add Course");
System.out.println("3. Enroll Student in Course");
System.out.println("4. View All Students");
System.out.println("5. View All Courses");
System.out.println("6. View All Enrollments");
System.out.println("7. View Courses of a Student");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
sc.nextLine(); // Consume newline
switch (choice) {
case 1 -> addStudent(conn, sc);
case 2 -> addCourse(conn, sc);
case 3 -> enrollStudent(conn, sc);
case 4 -> viewStudents(stmt);
case 5 -> viewCourses(stmt);
case 6 -> viewEnrollments(stmt);
case 7 -> viewStudentCourses(conn, sc);
case 8 -> System.out.println("Exiting...");
default -> System.out.println("Invalid choice.");
}
} while (choice != 8);
} catch (SQLException e) {
e.printStackTrace();
}
}
static void createTables(Statement stmt) throws SQLException {
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS students (" +
"id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100))");
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS courses (" +
"id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(100))");
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS enrollments (" +
"student_id INT, course_id INT, " +
"PRIMARY KEY (student_id, course_id), " +
"FOREIGN KEY (student_id) REFERENCES students(id), " +
"FOREIGN KEY (course_id) REFERENCES courses(id))");
}
static void addStudent(Connection conn, Scanner sc) throws SQLException {
System.out.print("Enter student name: ");
String name = sc.nextLine();
PreparedStatement ps = conn.prepareStatement("INSERT INTO students (name) VALUES (?)");
ps.setString(1, name);
ps.executeUpdate();
System.out.println("Student added.");
}
static void addCourse(Connection conn, Scanner sc) throws SQLException {
System.out.print("Enter course title: ");
String title = sc.nextLine();
PreparedStatement ps = conn.prepareStatement("INSERT INTO courses (title) VALUES (?)");
ps.setString(1, title);
ps.executeUpdate();
System.out.println("Course added.");
}
static void enrollStudent(Connection conn, Scanner sc) throws SQLException {
System.out.print("Enter student ID: ");
int sid = sc.nextInt();
System.out.print("Enter course ID: ");
int cid = sc.nextInt();
PreparedStatement ps = conn.prepareStatement("INSERT INTO enrollments (student_id, course_id) VALUES (?, ?)");
ps.setInt(1, sid);
ps.setInt(2, cid);
try {
ps.executeUpdate();
System.out.println("Enrollment successful.");
} catch (SQLIntegrityConstraintViolationException e) {
System.out.println("Enrollment failed (invalid IDs or already enrolled).");
}
}
static void viewStudents(Statement stmt) throws SQLException {
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
System.out.println("\n-- Students --");
while (rs.next()) {
System.out.println(rs.getInt("id") + ": " + rs.getString("name"));
}
}
static void viewCourses(Statement stmt) throws SQLException {
ResultSet rs = stmt.executeQuery("SELECT * FROM courses");
System.out.println("\n-- Courses --");
while (rs.next()) {
System.out.println(rs.getInt("id") + ": " + rs.getString("title"));
}
}
static void viewEnrollments(Statement stmt) throws SQLException {
String query = "SELECT s.name AS student, c.title AS course " +
"FROM enrollments e " +
"JOIN students s ON e.student_id = s.id " +
"JOIN courses c ON e.course_id = c.id";
ResultSet rs = stmt.executeQuery(query);
System.out.println("\n-- Enrollments --");
while (rs.next()) {
System.out.println(rs.getString("student") + " -> " + rs.getString("course"));
}
}
static void viewStudentCourses(Connection conn, Scanner sc) throws SQLException {
System.out.print("Enter student ID: ");
int sid = sc.nextInt();
PreparedStatement ps = conn.prepareStatement(
"SELECT c.title FROM enrollments e JOIN courses c ON e.course_id = c.id WHERE e.student_id = ?");
ps.setInt(1, sid);
ResultSet rs = ps.executeQuery();
System.out.println("\nCourses enrolled:");
while (rs.next()) {
System.out.println("- " + rs.getString("title"));
}
}
}