Java School Management System – Academic Planning and Scheduling

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

In this project, we will create a School Management System in Java using the Java Swing library and SQLite as the database. The Java School Management System will be built using the Eclipse IDE and will store the information of both students and teachers, including their ID, contact information, and address

About Java School Management System

The objective of this project is to create a School Management System using Java, Jswing, and SQLite in Eclipse. The Java School Management System will store and manage information of students and teachers such as their IDs, contacts, addresses, etc.

This project will walk you through the process of creating a graphical user interface using Jswing, connecting to an SQLite database, and implementing the functionalities for storing and retrieving data. By the end of this project, you will have a basic understanding of how to develop a School Management System using Java and SQLite.

Prerequisites for School Management System using Java

  • Basic knowledge of Java programming
  • Familiarity with Java Swing for creating GUI applications
  • Knowledge of databases and SQL queries.
  • Understanding of JAR files and how they are used in Java projects.
  • Understanding of database management and SQLite database.
  • Any Java IDE (Eclipse recommended as it is used in this project)
    To make GUI development easier, you can use the WindowBuilder plugin in Eclipse. WindowBuilder allows you to drag-and-drop GUI components and automatically generates the code for you, making it easier to create and design Java graphical user interfaces.

Download Java School Management System Project

Please download the source code of java school management system project: Java School Management System Project Code

  • Language used: Java
  • Coding Tool used: Eclipse IDE
  • Type: Desktop Application
  • Database used: SQLite Database

Steps to Create School Management System using Java

Following are the steps for developing the Java School Management System project:

Step 1:Creating the project and classes

  1. Open Eclipse and go to “File” > “New” > “Java Project”.
  2. Give a name to the project, e.g. “School Management System”.
  3. Right-click on the project and select “New” > “Class”.
  4. Give a name to the first class, “SchoolManagement”.
  5. Repeat steps 3 and 4 to create another class, “DB”.

Step 2:Adding SQLite in our project

  1. Open your Eclipse project and go to the Project Explorer.
  2. Right-click on the project name and select “Properties”.
  3. In the Properties window, select “Java Build Path” and then click on the “Libraries” tab.
  4. Click on the “Add External JARs” button and browse to the location where you have the SQLite JAR file.
  5. Select the JAR file and click “Open”.
  6. Click “OK” to close the Properties window.
  7. Now, the SQLite library is added to your Eclipse project.

Note: Before following these steps, make sure you have SQLite JAR file downloaded and saved in a local location. If not, you can download it from MavenRepository

Step 3:Creating the code to interact with database in the DB.java file we just created:

DB is a class that contains methods to initialize a SQLite database and manipulate its data.

1: Importing libraries

The first step is to import the necessary libraries that we will need. we will be using the following libraries:

package org.ProjectGurukul;

import java.sql.Statement;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.sqlite.SQLiteDataSource;

2: Initializing the database

dbInit(): This method initializes the database with two tables: studentDetails and teacherDetails. It sets the URL of the database to “jdbc:sqlite:school.db” and creates two tables if they do not exist. The method uses the SQLiteDataSource class to create the connection and Statement class to execute the SQL query.

The code for initializing the database is as follows:

public class DB {
//	declaring connection and datasource variables
  static Connection conn;
  static SQLiteDataSource ds;
  
//	initialize method to initialize the database with two tables :studentDetails and teacherDetails 
  public static void dbInit() {
    ds = new SQLiteDataSource();
    
    try {
            ds = new SQLiteDataSource();
            ds.setUrl("jdbc:sqlite:school.db");
        } catch ( Exception e ) {
            e.printStackTrace();
            
            System.exit(0);
        }
        try {
        	 conn = ds.getConnection();
        	 
        	 Statement stmt = conn.createStatement();
//        	 sql query to add student table
        	 String query = "CREATE TABLE IF NOT EXISTS studentDetails ( "
        	 				+ "student_id TEXT PRIMARY KEY ,"
        	 				+ "student_name TEXT,"
        	 				+ "student_contact TEXT,"
        	 				+ "student_dob TEXT,"
        	 				+ "student_gender TEXT,"
        	 				+ "student_address TEXT,"
        	 				+ "section TEXT"
        	 				+ " );";  	 
//        	 executing the query using statement variable
        	 stmt.executeUpdate(query);
//        	 for teacher table
        	 query = "CREATE TABLE IF NOT EXISTS teacherDetails ( "
 	 				+ "teacher_id TEXT PRIMARY KEY ,"
 	 				+ "teacher_name TEXT,"
 	 				+ "teacher_contact TEXT,"
 	 				+ "teacher_dob TEXT,"
 	 				+ "teacher_gender TEXT,"
 	 				+ "teacher_address TEXT,"
 	 				+ "salary TEXT"
 	 				+ " );";  	 
        	 stmt.executeUpdate(query);
        	 conn.close();
        	 
        } catch ( SQLException e ) {
            e.printStackTrace();
            System.exit( 0 );
        }
        ;
    }

3. Adding code to insert student and teacher data provided by the user into the database.

addStudent(String id, String name, String contact, String dob, String gender, String address, String section): This method inserts a new student’s data into the studentDetails table. The method takes seven parameters: student ID, name, contact, date of birth, gender, address, and section. It constructs an SQL query to insert the data into the table and executes it using the Statement class.

//	function to add the student into the database
  protected static void addStudent(String id,String name,String contact,
                   String dob,String gender,String address,
                   String section) throws SQLException {
    String query = "INSERT INTO studentDetails(student_id,student_name,student_contact,student_dob,student_gender,student_address,section) "
          + "VALUES("
            +"'"+ id +"',"
            +"'"+ name +"',"
            +"'"+ contact +"',"
            +"'"+ dob +"',"
            +"'"+ gender +"',"
            +"'"+ address +"',"
            +"'"+ section +"');" ;
    
    conn = ds.getConnection();
    Statement stmt =  conn.createStatement();
    stmt.executeUpdate(query);
    conn.close();
  }

addTeacher(String id, String name, String contact, String dob, String gender, String address, String salary): This method inserts a new teacher’s data into the teacherDetails table. The method takes seven parameters: teacher ID, name, contact, date of birth, gender, address, and salary. It constructs an SQL query to insert the data into the table and executes it using the Statement class.

//	function to add the Teacher into the database	
  protected static void addTeacher(String id,String name,String contact,
       String dob,String gender,String address,
       String salary) throws SQLException {
    String query = "INSERT INTO teacherDetails(teacher_id,teacher_name,teacher_contact,teacher_dob,teacher_gender,teacher_address,salary) "
            + "VALUES("
            +"'"+ id +"',"
            +"'"+ name +"',"
            +"'"+ contact +"',"
            +"'"+ dob +"',"
            +"'"+ gender +"',"
            +"'"+ address +"',"
            +"'"+ salary+"');" ;
            
    conn = ds.getConnection();
    Statement stmt =  conn.createStatement();
    stmt.executeUpdate(query);
    conn.close();
  }

4. Adding the code to delete Students and Teachers from the database.

deleteStudent(String id): This method deletes a student’s data from the studentDetails table. The method takes one parameter: student ID. It constructs an SQL query to delete the data from the table and executes it using the Statement class.

//	function to delete the student from the database
protected static void deleteStudent(String id) throws SQLException {
  String query = "DELETE FROM studentDetails WHERE student_id = '"+id+"';"; 
  conn = ds.getConnection();
  Statement stmt =  conn.createStatement();
  stmt.executeUpdate(query);
  conn.close();

}

deleteTeacher(String id): This method deletes a teacher’s data from the teacherDetails table. The method takes one parameter: teacher ID. It constructs an SQL query to delete the data from the table and executes it using the Statement class.

//	function to delete the teacher from the database
  protected static void deleteTeacher(String id) throws SQLException {
    String query = "DELETE FROM teacherDetails WHERE teacher_id = '"+id+"';"; 
    conn = ds.getConnection();
    Statement stmt =  conn.createStatement();
    stmt.executeUpdate(query);
    conn.close();
  
  }

5. Adding the code to search Teacher and Student in the database

searchTeacher(String id): It searches for a teacher in the database and returns the details of the teacher in a string array. The function takes a string parameter id which is the ID of the teacher to be searched. It forms a SQL query SELECT * FROM teacherDetails WHERE teacher_id = ‘”+id+”‘; to fetch the teacher details and executes it using a Statement object. The result of the query is stored in a ResultSet object, and the details of the teacher are fetched using the rs.getString method and stored in the result array. The function finally closes the database connection and returns the result array.

//	function that searches the teacher in the database and returns the result in string array
  protected static String[] searchTeacher(String id) throws SQLException {
    String query = "SELECT * FROM teacherDetails WHERE teacher_id = '"+id+"';"; 
    String[] result = new String[7];
    conn = ds.getConnection();
    Statement stmt =  conn.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    
    if(rs.next()) {
      result[0] = rs.getString("teacher_id");
      result[1] = rs.getString("teacher_name");
      result[2] = rs.getString("teacher_contact");
      result[3] = rs.getString("teacher_dob");
      result[4] = rs.getString("teacher_gender");
      result[5] = rs.getString("teacher_address");
      result[6] = rs.getString("salary");
    }
    
    conn.close();
    rs.close();
    return result;
  }

searchStudent(String id):It searches for a student in the database and returns the details of the student in a string array. The function takes a string parameter id which is the ID of the student to be searched. It forms a SQL query SELECT * FROM studentDetails WHERE student_id = ‘”+id+”‘; to fetch the student details and executes it using a Statement object. The result of the query is stored in a ResultSet object, and the details of the student are fetched using the rs.getString method and stored in the result array. The function finally closes the database connection and returns the result array.

//	function that searches the student in the database and returns the result in string array
  protected static String[] searchStudent(String id) throws SQLException {
    String query = "SELECT * FROM studentDetails WHERE student_id = '"+id+"';"; 
    String[] result = new String[7];
    conn = ds.getConnection();
    Statement stmt =  conn.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    
    if(rs.next()) {
      result[0] = rs.getString("student_id");
      result[1] = rs.getString("student_name");
      result[2] = rs.getString("student_contact");
      result[3] = rs.getString("student_dob");
      result[4] = rs.getString("student_gender");
      result[5] = rs.getString("student_address");
      result[6] = rs.getString("section");
    }
    
    conn.close();
    rs.close();
    return result;
  }

fetchData(DefaultTableModel model, String tableName):It fetches data from the database and adds it to a DefaultTableModel object which is used to update a JTable. The function takes two parameters, model which is the DefaultTableModel object and tableName which is the name of the database table from which the data is to be fetched. The function forms a SQL query SELECT * FROM “+ tableName +” ; to fetch all the data from the table and executes it using a Statement object. The result of the query is stored in a ResultSet object. The function then loops through the ResultSet and adds each row of data to the model object. The function finally closes the database connection and returns.

// function to fetch the data and add it to the model so that the jtable is updated 
  public static void fetchData(DefaultTableModel model, String tableName) throws SQLException {
    model.setRowCount(0);
    String query = "SELECT * FROM "+ tableName +" ;";
    conn = ds.getConnection();
    Statement stmt =  conn.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    if(tableName.equals("studentDetails")) {
      tableName = "student"; 
    }else {
      tableName="teacher";
    }
    while(rs.next()) {
      String id = rs.getString(tableName+"_id");
      String name = rs.getString(tableName+"_name");
      String contact = rs.getString(tableName+"_contact");
      String dob = rs.getString(tableName+"_dob");
      String gender = rs.getString(tableName+"_gender");
      String address = rs.getString(tableName+"_address");
      String data;
      if(tableName.equals("student")) {
        data = rs.getString("section"); 
      }else {
        data = rs.getString("salary");
      }
      model.addRow(new Object[]{id,name,contact,dob,gender,address,data});
      
    }
    
    conn.close();
    rs.close();
    
  }
}

Step 4:Adding the UI code and implementing ActionListeners to call the respective methods from the DB class in the ‘StudentManagement.java’ file/class :

Following is the code for the StudentManagement.java file:

package org.ProjectGurukul;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.JScrollPane;


public class SchoolManagement {

  private JFrame frmSchoolManagementSystem;
  private JTextField studentNameTextField;
  private JTextField studentContactTextField;
  private JTextField studentIdTextField;
  private JTextField studentDobTextField;
  private JTextField teacherNameTextField;
  private JTextField teacherContactTextField;
  private JTextField teacherIDTextField;
  private JTextField teacherDobtextField;
  private JTextField studentSectiontextField;
  private JTextField salarytextField;
  private JTextField keywordTextField;
  private JTable table;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
//					Calling the init mehtod to initialize the database if not already initialized
          DB.dbInit();;
          SchoolManagement window = new SchoolManagement();
          window.frmSchoolManagementSystem.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */
  
  public SchoolManagement() {
    initialize();
  }

  /**
   * Initialize the contents of the frame.
   */
  
  private void initialize() {
//		creating the table model for jtable
    DefaultTableModel model = new DefaultTableModel();
    model.addColumn("Id");
    model.addColumn("Name");
    model.addColumn("Contact");
    model.addColumn("D.O.B");
    model.addColumn("Gender");
    model.addColumn("Address");
    model.addColumn("Salary");
    
//		creating new frame for all the components 
    frmSchoolManagementSystem = new JFrame();
    frmSchoolManagementSystem.setTitle("School Management System by ProjectGurukul");
    frmSchoolManagementSystem.setBounds(100, 100, 674, 684);
    frmSchoolManagementSystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
    frmSchoolManagementSystem.getContentPane().setLayout(new BoxLayout(frmSchoolManagementSystem.getContentPane(), BoxLayout.Y_AXIS));
    
//		tabbed pane to hold teacher and student panels
    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    
//		adding the tabbed pane to the content pane
    frmSchoolManagementSystem.getContentPane().add(tabbedPane);
    
//		creating the student and teacher panel and adding it to the tabbed pane
    JPanel studentPanel = new JPanel();
    tabbedPane.addTab("Student Panel", null, studentPanel, null);
    studentPanel.setBackground(new Color(46, 194, 126));
    studentPanel.setLayout(null);
    
    JPanel teacherPanel = new JPanel();
    teacherPanel.setBackground(new Color(230, 97, 0));
    tabbedPane.addTab("Teacher Panel",teacherPanel);
    teacherPanel.setLayout(null);
    
//		creating the UI for the teacher panel 
    teacherNameTextField = new JTextField();
    teacherNameTextField.setBounds(88, 34, 213, 24);
    teacherNameTextField.setColumns(10);
    teacherPanel.add(teacherNameTextField);
    
    JLabel lblNewLabel_5 = new JLabel("Name : ");
    lblNewLabel_5.setBounds(0, 34, 70, 15);
    teacherPanel.add(lblNewLabel_5);
    
    teacherContactTextField = new JTextField();
    teacherContactTextField.setBounds(88, 65, 213, 24);
    teacherContactTextField.setColumns(10);
    teacherPanel.add(teacherContactTextField);
    
    JLabel lblNewLabel_1_2 = new JLabel("Contact : ");
    lblNewLabel_1_2.setBounds(0, 66, 70, 15);
    teacherPanel.add(lblNewLabel_1_2);
    
    teacherIDTextField = new JTextField();
    teacherIDTextField.setBounds(88, 101, 213, 24);
    teacherIDTextField.setColumns(10);
    teacherPanel.add(teacherIDTextField);
    
    JRadioButton rdbtnMale_1 = new JRadioButton("Male");
    rdbtnMale_1.setBounds(106, 184, 75, 23);
    rdbtnMale_1.setBackground(null);
    teacherPanel.add(rdbtnMale_1);
    
    JRadioButton rdbtnFemale_1 = new JRadioButton("Female");
    rdbtnFemale_1.setBounds(201, 184, 100, 23);
    rdbtnFemale_1.setBackground(null);
    teacherPanel.add(rdbtnFemale_1);
    
    ButtonGroup buttonGroup_1 = new ButtonGroup();
    buttonGroup_1.add(rdbtnMale_1);
    buttonGroup_1.add(rdbtnFemale_1);
    
    JLabel lblNewLabel_2_1 = new JLabel("Teacher ID : ");
    lblNewLabel_2_1.setBounds(0, 101, 100, 15);
    teacherPanel.add(lblNewLabel_2_1);
    
    JLabel lblNewLabel_3_1 = new JLabel("Gender :");
    lblNewLabel_3_1.setBounds(0, 184, 70, 15);
    teacherPanel.add(lblNewLabel_3_1);
    
    JLabel lblNewLabel_4_1 = new JLabel("Address : ");
    lblNewLabel_4_1.setBounds(0, 221, 100, 15);
    teacherPanel.add(lblNewLabel_4_1);
    
    teacherDobtextField = new JTextField();
    teacherDobtextField.setBounds(88, 137, 213, 24);
    teacherDobtextField.setColumns(10);
    teacherPanel.add(teacherDobtextField);
    
    JLabel lblNewLabel_1_1_1 = new JLabel("D.O.B.");
    lblNewLabel_1_1_1.setBounds(0, 138, 70, 15);
    teacherPanel.add(lblNewLabel_1_1_1);
    
    JTextArea teacherAddressTextarea = new JTextArea();
    teacherAddressTextarea.setBounds(88, 221, 213, 93);
    teacherPanel.add(teacherAddressTextarea);
    
    JLabel salaryLabel = new JLabel("Salary  : ");
    salaryLabel.setBounds(361, 38, 70, 15);
    teacherPanel.add(salaryLabel);
    
    salarytextField = new JTextField();
    salarytextField.setColumns(10);
    salarytextField.setBounds(431, 34, 213, 24);
    teacherPanel.add(salarytextField);
    
//		adding the buttons and assigning the action listener to them 
    JButton btnAddTeacher = new JButton("Add Teacher");
    btnAddTeacher.setBounds(318, 268, 135, 34);
    teacherPanel.add(btnAddTeacher);
    btnAddTeacher.addActionListener(new ActionListener() {
          
          @Override
          public void actionPerformed(ActionEvent e) {
            String gender = "Female";
            if(rdbtnMale_1.isSelected()) {
              gender = "Male";
            }
            try {
//							callin the methods to add the dataq in text fields to the database and then updating the jtable
              DB.addTeacher(teacherIDTextField.getText(), teacherNameTextField.getText(), teacherContactTextField.getText(), teacherDobtextField.getText(), gender, teacherAddressTextarea.getText(), salarytextField.getText());
              DB.fetchData(model,"teacherDetails");

            } catch (SQLException e1) {
              // showing the error messsage
              JOptionPane.showMessageDialog(teacherPanel,"Teacher Already exists or Error","ERROR", JOptionPane.ERROR_MESSAGE);
              e1.printStackTrace();
            }
            
          }
        });
    
    JButton btnSearchTeacher = new JButton("Search Teacher");
    btnSearchTeacher.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        model.setRowCount(0);
        
        try {
          String[] rs =
          DB.searchTeacher(keywordTextField.getText());
//					code for not found error display 
          if(rs[0] == null) {
            JOptionPane.showMessageDialog(teacherPanel,"NOT Found","ERROR", JOptionPane.ERROR_MESSAGE);
          }else {
            model.addRow(new Object[] {rs[0],rs[1],rs[2],rs[3],rs[4],rs[5],rs[6]});
          }
        } catch (SQLException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
      }
    });
    btnSearchTeacher.setBounds(482, 268, 175, 34);
    teacherPanel.add(btnSearchTeacher);
    
    JButton btnDeleteTeacher = new JButton("Delete Teacher");
    btnDeleteTeacher.setBounds(482, 220, 175, 34);
    teacherPanel.add(btnDeleteTeacher);
    btnDeleteTeacher.addActionListener(new ActionListener() {
      
      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try {
//					calling the method to delete the teacher 
          DB.deleteTeacher(keywordTextField.getText());
          DB.fetchData(model,"teacherDetails");

        } catch (SQLException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
      }
    });
    
//		Creating the ui for the student panel 
    studentNameTextField = new JTextField();
    studentNameTextField.setBounds(93, 27, 213, 24);
    studentPanel.add(studentNameTextField);
    studentNameTextField.setColumns(10);
    
    JLabel studentNameLabel = new JLabel("Name : ");
    studentNameLabel.setBounds(5, 27, 70, 15);
    studentPanel.add(studentNameLabel);
    
    studentContactTextField = new JTextField();
    studentContactTextField.setColumns(10);
    studentContactTextField.setBounds(93, 58, 213, 24);
    studentPanel.add(studentContactTextField);
    
    JLabel studentContactLabel = new JLabel("Contact : ");
    studentContactLabel.setBounds(5, 59, 70, 15);
    studentPanel.add(studentContactLabel);
    
    studentIdTextField = new JTextField();
    studentIdTextField.setColumns(10);
    studentIdTextField.setBounds(93, 94, 213, 24);
    studentPanel.add(studentIdTextField);
    
    JRadioButton rdbtnMale = new JRadioButton("Male");
    rdbtnMale.setBackground(null);
    rdbtnMale.setBounds(111, 177, 75, 23);
    studentPanel.add(rdbtnMale);
    
    JRadioButton rdbtnFemale = new JRadioButton("Female");
    rdbtnFemale.setBackground(null);
    rdbtnFemale.setBounds(206, 177, 100, 23);
    studentPanel.add(rdbtnFemale);
    
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(rdbtnFemale);
    buttonGroup.add(rdbtnMale);
    
    JLabel studentIdLabel = new JLabel("Student ID : ");
    studentIdLabel.setBounds(5, 94, 100, 15);
    studentPanel.add(studentIdLabel);
    
    JLabel studentGenderLabel = new JLabel("Gender :");
    studentGenderLabel.setBounds(5, 177, 70, 15);
    studentPanel.add(studentGenderLabel);
    
    JLabel studentAddressLabel = new JLabel("Address : ");
    studentAddressLabel.setBounds(5, 214, 100, 15);
    studentPanel.add(studentAddressLabel);
    
    studentDobTextField = new JTextField();
    studentDobTextField.setColumns(10);
    studentDobTextField.setBounds(93, 130, 213, 24);
    studentPanel.add(studentDobTextField);
    
    JLabel dobLabel= new JLabel("D.O.B.");
    dobLabel.setBounds(5, 131, 70, 15);
    studentPanel.add(dobLabel);
    
    JTextArea addresstextarea = new JTextArea();
    addresstextarea.setBounds(93, 218, 213, 93);
    studentPanel.add(addresstextarea);
    
    JLabel sectionLabel = new JLabel("Section  : ");
    sectionLabel.setBounds(374, 31, 70, 15);
    studentPanel.add(sectionLabel);
    
    studentSectiontextField = new JTextField();
    studentSectiontextField.setColumns(10);
    studentSectiontextField.setBounds(444, 27, 213, 24);
    studentPanel.add(studentSectiontextField);
    
    JButton btnAddStudent = new JButton("Add Student");
    btnAddStudent.setBounds(318, 268, 135, 34);
    studentPanel.add(btnAddStudent);
    btnAddStudent.addActionListener(new ActionListener() {
      
      @Override
      public void actionPerformed(ActionEvent e) {
        String gender = "Female";
        if(rdbtnMale.isSelected()) {
          gender = "Male";
        }
        try {
//					calling the methods to add the dataq in text fields to the database and then updating the jtable
          DB.addStudent(studentIdTextField.getText(), studentNameTextField.getText(), studentContactTextField.getText(), studentDobTextField.getText(), gender, addresstextarea.getText(), studentSectiontextField.getText());
          DB.fetchData(model,"studentDetails");
        } catch (SQLException e1) {
          // showing the error message if student already exists in the database
          JOptionPane.showMessageDialog(studentPanel,"Student Already exists or Error","ERROR", JOptionPane.ERROR_MESSAGE);

          e1.printStackTrace();
        }
        
      }
    });
    
    JButton btnSearchStudent = new JButton("Search Student");
    btnSearchStudent.setBounds(482, 268, 175, 34);
    studentPanel.add(btnSearchStudent);
    btnSearchStudent.addActionListener(new ActionListener() {
      
      @Override
      public void actionPerformed(ActionEvent e) {
        model.setRowCount(0);
        
        try {
          String[] rs =
          DB.searchStudent(keywordTextField.getText());
//					to display the search item not found error 
          if(rs[0] == null) {
            JOptionPane.showMessageDialog(studentPanel,"NOT Found","ERROR", JOptionPane.ERROR_MESSAGE);
          }else {
            model.addRow(new Object[] {rs[0],rs[1],rs[2],rs[3],rs[4],rs[5],rs[6]});
          }
          
        } catch (SQLException e1) {
          // TODO Auto-generated catch block
          
          e1.printStackTrace();
        }
      }
        
      });
    
    
    JButton btnDeleteStudent = new JButton("Delete Student");
    btnDeleteStudent.setBounds(482, 220, 175, 34);
    studentPanel.add(btnDeleteStudent);
    btnDeleteStudent.addActionListener(new ActionListener() {
      
      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try {
//					calling the method to delete the student 
          DB.deleteStudent(keywordTextField.getText());
          DB.fetchData(model,"studentDetails");

        } catch (SQLException e1) {
          // TODO Auto-generated catch block
          
          e1.printStackTrace();
        }
      }
    });
    
//		output panel Ui  
    JPanel outputPanel = new JPanel();
    outputPanel.setBackground(new Color(36, 31, 49));
    frmSchoolManagementSystem.getContentPane().add(outputPanel);
    outputPanel.setLayout(null);
    
    JLabel keywordLabel = new JLabel("Enter ID  : ");
    keywordLabel.setBounds(12, 11, 138, 15);
    keywordLabel.setForeground(new Color(255, 255, 255));
    outputPanel.add(keywordLabel);
    
    keywordTextField = new JTextField();
    keywordTextField.setBounds(92, 8, 227, 22);
    keywordTextField.setColumns(10);
    outputPanel.add(keywordTextField);
    
//		new jtable in output panel 
    table = new JTable(model);
    table.setVisible(true);
    table.setEnabled(false);
    
//		adding the jtable to jscrollpane
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(12, 38, 630, 251);
    outputPanel.add(scrollPane);
  }
}

The given code implements a series of action listeners for buttons in a student management system. The code creates three buttons – “Add Student”, “Search Student”, and “Delete Student”. For each button, an action listener is attached to it. When a user clicks on a button, the corresponding action listener’s actionPerformed method is executed.

The first button “Add Student” is created using JButton btnAddStudent = new JButton(“Add Student”);. An action listener is added to it using btnAddStudent.addActionListener(new ActionListener() {…}). When the “Add Student” button is clicked, the actionPerformed method of the action listener is executed. The code inside this method performs the following steps:

  • First, it sets a default value of “Female” to a gender variable.
  • Then, it checks if the male radio button is selected and sets the gender variable to “Male” if it is.
  • The code then calls the DB.addStudent method and passes it several parameters like the student ID, name, contact number, date of birth, gender, address, and section.
  • After adding the student to the database, the code calls the DB.fetchData method and updates the JTable with the new data.
  • In case of an error such as if the student already exists in the database, an error message is displayed using the JOptionPane.showMessageDialog method.

The second button “Search Student” is created and an action listener is added to it in a similar manner. When the “Search Student” button is clicked, the actionPerformed method of its action listener is executed. This method performs the following steps:

  • First, it sets the row count of the JTable model to zero.
  • Then, it calls the DB.searchStudent method and passes it a keyword to search for the student.
  • If the search result is not found, an error message is displayed.
  • If the search result is found, the returned data is added to a row in the JTable model.

The third button “Delete Student” is created and an action listener is added to it in a similar manner. When the “Delete Student” button is clicked, the actionPerformed method of its action listener is executed. This method performs the following steps:

  • It calls the DB.deleteStudent method and passes it a keyword for the student to be deleted.
  • After deleting the student, the code calls the DB.fetchData method to update the JTable with the new data.

Note that this process of creating buttons, adding action listeners, and executing the desired functionality is replicated for “Add Teacher”, “Search Teacher”, and “Delete Teacher” functionality as well.

Java School Management System Output

java school management system output

school management system output

Summary

In this project, we have created a School management system using Java and Swing for the GUI. The system has two panels, one for students and one for teachers. Each panel contains text fields for entering the name, contact information, date of birth, and other relevant details of a student or teacher. The system uses a JTabbedPane to switch between the two panels. A JTable is used to display the information stored in the system, and a JButton is used to perform various actions, such as adding a new student or teacher to the database.

The system connects to a database using the DB class to store and retrieve information. The database is initialized when the application starts by calling the “DB.dbInit()” method.The database uses SQlite and we have learn how we can incorporate that into our project.

2 Responses

  1. Sanvi Siri says:

    Good

  2. Sanvi Siri says:

    I want ppt and report

Leave a Reply

Your email address will not be published. Required fields are marked *