Create database for school system
Here's a basic example of a database for a school system in Java using MySQL as the database management system. This example includes classes for `Student`, `Teacher`, `Course`, `Grade`, and `School`. **Database Schema:** ```sql CREATE TABLE School ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, address VARCHAR(255) NOT NULL ); CREATE TABLE Teacher ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, school_id INT, FOREIGN KEY (school_id) REFERENCES School(id) ); CREATE TABLE Course ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL, teacher_id INT, FOREIGN KEY (teacher_id) REFERENCES Teacher(id) ); CREATE TABLE Student ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, school_id INT, FOREIGN KEY (school_id) REFERENCES School(id) ); CREATE TABLE Grade ( id INT PRIMARY KEY AUTO_INCREMENT, student_id INT, course_id INT, grade DECIMAL(3, 2) NOT NULL, FOREIGN KEY (student_id) REFERENCES Student(id), FOREIGN KEY (course_id) REFERENCES Course(id) ); ``` **Java Classes:** ```java // School.java public class School { private int id; private String name; private String address; public School(int id, String name, String address) { this.id = id; this.name = name; this.address = address; } public int getId() { return id; } public String getName() { return name; } public String getAddress() { return address; } } // Teacher.java public class Teacher { private int id; private String name; private String email; private School school; public Teacher(int id, String name, String email, School school) { this.id = id; this.name = name; this.email = email; this.school = school; } public int getId() { return id; } public String getName() { return name; } public String getEmail() { return email; } public School getSchool() { return school; } } // Course.java public class Course { private int id; private String name; private String description; private Teacher teacher; public Course(int id, String name, String description, Teacher teacher) { this.id = id; this.name = name; this.description = description; this.teacher = teacher; } public int getId() { return id; } public String getName() { return name; } public String getDescription() { return description; } public Teacher getTeacher() { return teacher; } } // Student.java public class Student { private int id; private String name; private String email; private School school; public Student(int id, String name, String email, School school) { this.id = id; this.name = name; this.email = email; this.school = school; } public int getId() { return id; } public String getName() { return name; } public String getEmail() { return email; } public School getSchool() { return school; } } // Grade.java public class Grade { private int id; private Student student; private Course course; private double grade; public Grade(int id, Student student, Course course, double grade) { this.id = id; this.student = student; this.course = course; this.grade = grade; } public int getId() { return id; } public Student getStudent() { return student; } public Course getCourse() { return course; } public double getGrade() { return grade; } } ``` **Database Connection and CRUD Operations:** ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class SchoolDatabase { private Connection connection; public SchoolDatabase(String dbUrl, String dbUser, String dbPassword) { try { connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword); } catch (SQLException e) { System.out.println("Error connecting to database: " + e.getMessage()); } } public void createSchool(School school) { try { String query = "INSERT INTO School (name, address) VALUES (?, ?)"; PreparedStatement statement = connection.prepareStatement(query); statement.setString(1, school.getName()); statement.setString(2, school.getAddress()); statement.executeUpdate(); } catch (SQLException e) { System.out.println("Error creating school: " + e.getMessage()); } } public void createTeacher(Teacher teacher) { try { String query = "INSERT INTO Teacher (name, email, school_id) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(query); statement.setString(1, teacher.getName()); statement.setString(2, teacher.getEmail()); statement.setInt(3, teacher.getSchool().getId()); statement.executeUpdate(); } catch (SQLException e) { System.out.println("Error creating teacher: " + e.getMessage()); } } public void createCourse(Course course) { try { String query = "INSERT INTO Course (name, description, teacher_id) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(query); statement.setString(1, course.getName()); statement.setString(2, course.getDescription()); statement.setInt(3, course.getTeacher().getId()); statement.executeUpdate(); } catch (SQLException e) { System.out.println("Error creating course: " + e.getMessage()); } } public void createStudent(Student student) { try { String query = "INSERT INTO Student (name, email, school_id) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(query); statement.setString(1, student.getName()); statement.setString(2, student.getEmail()); statement.setInt(3, student.getSchool().getId()); statement.executeUpdate(); } catch (SQLException e) { System.out.println("Error creating student: " + e.getMessage()); } } public void createGrade(Grade grade) { try { String query = "INSERT INTO Grade (student_id, course_id, grade) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(query); statement.setInt(1, grade.getStudent().getId()); statement.setInt(2, grade.getCourse().getId()); statement.setDouble(3, grade.getGrade()); statement.executeUpdate(); } catch (SQLException e) { System.out.println("Error creating grade: " + e.getMessage()); } } public void closeConnection() { try { connection.close(); } catch (SQLException e) { System.out.println("Error closing connection: " + e.getMessage()); } } } ``` **Example Usage:** ```java public class Main { public static void main(String[] args) { SchoolDatabase database = new SchoolDatabase("jdbc:mysql://localhost:3306/school", "username", "password"); School school = new School(1, "Example School", "123 Main St"); database.createSchool(school); Teacher teacher = new Teacher(1, "John Doe", "john.doe@example.com", school); database.createTeacher(teacher); Course course = new Course(1, "Math", "Math course", teacher); database.createCourse(course); Student student = new Student(1, "Jane Doe", "jane.doe@example.com", school); database.createStudent(student); Grade grade = new Grade(1, student, course, 85.0); database.createGrade(grade); database.closeConnection(); } } ``` This is a basic example of a school database system in Java. It includes classes for `School`, `Teacher`, `Course`, `Student`, and `Grade`, as well as a `SchoolDatabase` class for interacting with the database. The example usage demonstrates how to create a school, teacher, course, student, and grade, and then close the database connection.