Student Management System In Java

Home » Uncategorized » Student Management System In Java

The Student Management System in Java is a simple Java project that creates a student information management system. In this project, we will design the framework to take input from the user to collect student details such as Enrollment No., Name, Class, Subject, and Attendance. And then, we will be storing these details in the database through Add Record and Update Record buttons. This system is helpful for colleges to manage student details in one place and perform tasks accordingly.

Introduction

Let’s look at the functioning of our basic digital school register. Our application helps colleges and institutions keep track of students and their related information. The program opens up a window on your screen. In that window, you can type in a student’s information, such as their ID, name, class, subjects, and attendance.

It has two buttons – one to add new student information and one to update existing student information. If you click on a student’s name in the list (table), their information will appear in the text fields at the top, so you can see or change it.

Now the program talks to a database (which is like a big, organized digital cupboard where all this student information is stored). When you add or update information, it saves it to the database. It also fetches and shows information from the database in a table in the window. So you can see all students’ details at once.

So, it’s a handy tool for managing student information! To make the project more interesting, we will implement a simple GUI and use an SQL database to store information regarding student names, classes, subjects & attendance.

Objectives

We have developed this Student Management System in Java to achieve the following objectives:

1. Create and display a Graphical User Interface (GUI) window where users can interact with the student data.

2. Allow the user to input student information like ID, Name, Class, Subjects, and Attendance using the GUI.

3. Provide a way to add new student records to a database by filling in the relevant details and clicking the ‘Add Record’ button.

4. Offer a method to update existing student records in the database using the ‘Update Record’ button.

5. Enable the selection of a student record from the data table, which then populates the input fields with the selected student’s data for viewing or editing.

6. Connect to and interact with a MySQL database, allowing the program to retrieve, add, and update student records in the database.

7. Refresh the data table in the GUI window automatically whenever a new record is added or an existing record is updated, ensuring that the displayed data is always up-to-date.

8. Handle errors and exceptions, providing useful feedback to the user through dialog messages.

Requirements

To successfully build and run this student management system in Java, the following technical prerequisites are required:

1. Java Development Kit (JDK): You need to have the Java Development Kit installed on your system. The version should be compatible with the version used in the code.

2. Java Integrated Development Environment (IDE): A Java IDE such as IntelliJ IDEA, Eclipse, or NetBeans is required to write and compile the code.

3. Swing Library: The Swing library is used for creating the Graphical User Interface (GUI) in this code. Swing is included in the standard Java Development Kit (JDK), so you won’t need to install anything extra if you have JDK.

4. MySQL Database: The code interacts with a MySQL database to store and retrieve student data, so you need to have a MySQL server running. The database should have the correct schema that the code is designed to interact with.

5. MySQL JDBC Driver: This is needed to facilitate the connection between the Java application and the MySQL database. The code use “com.mysql.cj.jdbc.Driver” which is a part of MySQL Connector/J. You’ll need to include this in your project’s classpath.

6. Understanding of SQL: You need to understand SQL queries as the code uses SQL for interacting with the database.

7. Knowledge of Java: A good understanding of Java programming language, including its object-oriented programming concepts, exception handling, event handling, and Swing library, is required to comprehend, modify, or build upon this code.

[Please note that the exact versions of the JDK, MySQL server, and MySQL JDBC Driver required may depend on the specific details of your project or environment]

Source Code

package com.company;
public class Main {
public static void main(String[] args) {
new Student();
}
}
package com.company;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.util.Vector;
public class Student {
private JTextField idData;
private JTextField nameData;
private JTable table1;
private JButton ADDRECORDButton;
private JButton UPDATERECORDButton;
private JComboBox classData;
private JTextField attendData;
private JComboBox subData;
private JPanel studentPanel;
JFrame student = new JFrame();
public Student() {
// Setting up the GUI window
student.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
student.setContentPane(studentPanel);
student.pack();
student.setLocationRelativeTo(null);
student.setSize(600, 500);
student.setVisible(true);
// Populate table with data
tableData();
ADDRECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Add new record button clicked
if (idData.getText().equals("") || nameData.getText().equals("") || attendData.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please Fill All Fields to add Record.");
} else {
try {
// Insert new record into the database
String sql = "INSERT INTO student (ENROLLMENT_NO, NAME, CLASS, SUBJECTS, ATTENDANCE) " +
"VALUES (?, ?, ?, ?, ?)";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern", "root", "root");
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, Integer.parseInt(idData.getText()));
statement.setString(2, nameData.getText());
statement.setString(3, "" + classData.getSelectedItem());
statement.setString(4, "" + subData.getSelectedItem());
statement.setString(5, attendData.getText() + "%");
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "ITEM ADDED SUCCESSFULLY");
idData.setText("");
nameData.setText("");
attendData.setText("");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
// Refresh table data
tableData();
}
}
});
UPDATERECORDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Update record button clicked
try {
// Update the selected record in the database
String sql = "UPDATE student " +
"SET NAME = '" + nameData.getText() + "', CLASS = '" + classData.getSelectedItem() +
"', SUBJECTS = '" + subData.getSelectedItem() + "', ATTENDANCE = '" + attendData.getText() + "'" +
" WHERE ENROLLMENT_NO = " + Integer.parseInt(idData.getText());
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern", "root", "root");
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "Updated successfully");
} catch (Exception e2) {
System.out.println(e2);
}
// Refresh table data
tableData();
}
});
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Mouse clicked on table row
DefaultTableModel dm = (DefaultTableModel) table1.getModel();
int selectedRow = table1.getSelectedRow();
idData.setText(dm.getValueAt(selectedRow, 0).toString());
nameData.setText(dm.getValueAt(selectedRow, 1).toString());
String[] a = dm.getValueAt(selectedRow, 4).toString().split("%");
attendData.setText(a[0]);
}
});
}
public void tableData() {
try {
// Retrieve student data from the database
String a = "SELECT * FROM student";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern", "root", "root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(a);
// Set up the table model with the retrieved data
table1.setModel(buildTableModel(rs));
} catch (Exception ex1) {
JOptionPane.showMessageDialog(null, ex1.getMessage());
}
}
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// Get column names
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// Get table data
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}

Explanation of the Code

Let’s see the GUI first:

1. We have used JFrame to generate a GUI window and configure numerous components, such as text boxes, buttons, and a table to display student data. 

2. The ADDRECORDButton’ allows the user to enter a new student record into the database. It collects the user’s data, builds a SQL put query, and executes it to put the data into the ‘student’ table.

3. The UPDATERECORDButton allows the user to change an existing student record in the database. It gets the user’s new data, creates a SQL UPDATE statement, and executes it to update the appropriate entry in the student table.

4. The ‘tableData method pulls all student records from the database and uses a DefaultTableModel to populate the table with data.

5. The buildTableModel method turns a ResultSet object into a DefaultTableModel’, which is then used to create the table with the retrieved data.

Moving to retrieving information from the SQL database & updating it according to the user’s input, we will apply the following:

1. A “MouseListener” is added to the table. When a row in the table is clicked, it fills the input fields with the corresponding student data.

2. The “tableData” method retrieves all student records from the database and populates the table with these records.

3. The buildTableModel method is a utility function that converts a ResultSet from a database query into a DefaultTableModel which can be used by a JTable.

4. We have used MySQL as our database and the JDBC driver to connect to and interact with the database.

5. The connection details (URL, username, password) for the database are hardcoded in the program.

Output

Here is the GUI of our Student Management System in Java:

Student Management System Java Project

Figure 1: Student Page

Conclusion

Hence, we have successfully designed a Student Management System in Java that helps to manage students’ information. This code provides a simple yet functional student record system with a GUI, database interaction, and error handling. This system is very helpful for colleges as it is a very efficient way to store & get access to the student information as it contains all the necessary information like the subjects chosen in class & current attendance.

More Java Projects>>>

You May Also Like To Create…