You are on page 1of 18

JDBC CONNECTIVITY FOR

STUDENT INFORMATION
MANAGEMENT
SYSTEM
AIM:
To Implement JDBC (Java Database Connectivity) for a student information Management
System which involves establishing a connection between the Java application and the
database.

DESCRIPTION:
This project involves the implementation of a robust Student Information Management
System (SIMS) using Java Database Connectivity (JDBC) to establish a seamless connection
between the Java application and a MySQL database. The SIMS serves as a comprehensive
solution for educational institutions, enabling efficient storage, retrieval, and management of
student information.

Key Features:

User-Friendly GUI: The system boasts an intuitive Graphical User Interface (GUI) developed
using Swing, providing a seamless and user-friendly experience for administrators and end-
users.

JDBC Integration: Leveraging JDBC, the system establishes a secure and efficient
connection to a MySQL database, ensuring the seamless flow of data between the application
and the backend.

Database Schema: The MySQL database is designed to accommodate essential student


information, including unique student IDs, names, and enrolled courses. The system employs
JDBC to perform database operations such as insertion, retrieval, and updating of student
records.

Addition of Student Records: Administrators can effortlessly add new student records to the
system by entering relevant details such as student ID, name, and course. The JDBC
integration ensures the immediate and accurate storage of this information in the database.

J. Bhavatharni (71812201030) 1
Viewing Student Records: The system facilitates the retrieval and display of student records
stored in the database. Users can easily view a list of students along with their respective
details, providing a comprehensive overview of the enrolled student body.

Data Validation and Error Handling: The system incorporates data validation mechanisms to
ensure the integrity of information entered. Additionally, robust error-handling procedures
are implemented to gracefully manage unforeseen issues, providing informative feedback to
users.

Scalability: The SIMS is designed with scalability in mind, capable of handling a growing
volume of student data and additional features as the educational institution evolves.

Security Measures: Security best practices are implemented to protect sensitive student
information. The JDBC connection is secured, and user authentication mechanisms are in
place to prevent unauthorized access to the system.

Optimized Performance: Performance optimizations, such as connection pooling and


efficient query execution, are implemented through JDBC to enhance the overall
responsiveness of the system.

Documentation: Comprehensive documentation is provided, covering aspects such as system


architecture, database schema, and step-by-step instructions for setting up and configuring the
SIMS with the MySQL database.

Expected Outcomes:

Efficient Data Management: A streamlined process for entering, retrieving, and managing
student information within the educational institution.

Improved Decision-Making: Enhanced access to student data empowers administrators to


make informed decisions related to academic planning and resource allocation.

User Satisfaction: The user-friendly interface and seamless integration with the database
contribute to overall user satisfaction among administrators and end-users.

J. Bhavatharni (71812201030) 2
Secure and Reliable System: Implementation of security measures ensures the confidentiality
and integrity of student information, contributing to the reliability of the SIMS.

This project not only addresses the fundamental aspects of student information management
but also focuses on leveraging JDBC for a robust, scalable, and secure solution tailored to the
needs of educational institutions.

IMPLEMENTATION:
LoginFramme.java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class LoginFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
public LoginFrame() {
setTitle("Login");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JLabel usernameLabel = new JLabel("Username:");
usernameField = new JTextField(15);
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField(15);
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username =
usernameField.getText();
char[] passwordChars = passwordField.getPassword();

J. Bhavatharni (71812201030) 3
String password = new String(passwordChars);
// Validate the login credentials
if (validateLogin(username, password)) {
openHospitalManagement();
} else {
JOptionPane.showMessageDialog(LoginFrame.this, "Invalid username or
password",

"Login Failed", JOptionPane.ERROR_MESSAGE);


}
// Clear the password field for security
passwordField.setText("");
}
});
setLayout(new GridLayout(3, 2));
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(new JLabel()); // Empty label for spacing
add(loginButton);
setVisible(true);
}
private boolean validateLogin(String username, String password) {
// Your login validation logic goes here
// For demonstration purposes, I'm using hardcoded values
return "hospital".equals(username) && "hospital123".equals(password);
}
private void openHospitalManagement() {
dispose(); // Close the login frame
new Hospitalmanagement();
}

J. Bhavatharni (71812201030) 4
public static void main(String[] args) {
SwingUtilities.invokeLater(LoginFrame::new);
}
}

Output:

appGUI.java

import javax.swing.*;

import java.awt.event.*;

import java.sql.*;

public class AppGUI extends JFrame implements ActionListener {

private final JLabel studentIdLabel, firstNameLabel, lastNameLabel, majorLabel,


phoneLabel, gpaLabel, dobLabel;

private final JTextField studentIdField, firstNameField, lastNameField, majorField,


phoneField, gpaField, dobField;

private final JButton addButton, displayButton, sortButton, searchButton, modifyButton;

private Statement stmt;

public AppGUI() {

J. Bhavatharni (71812201030) 5
JFrame frame = new JFrame("Student Database");

JPanel panel = new JPanel();

// Initialize labels

studentIdLabel = new JLabel("Student ID:");

firstNameLabel = new JLabel("First Name:");

lastNameLabel = new JLabel("Last Name:");

majorLabel = new JLabel("Major:");

phoneLabel = new JLabel("Phone:");

gpaLabel = new JLabel("GPA:");

dobLabel = new JLabel("Date of Birth (yyyy-mm-dd):");

// Initialize text fields

studentIdField = new JTextField(10);

firstNameField = new JTextField(10);

lastNameField = new JTextField(10);

majorField = new JTextField(10);

phoneField = new JTextField(10);

gpaField = new JTextField(10);

dobField = new JTextField(10);

// Initialize buttons

addButton = new JButton("Add");

displayButton = new JButton("Display");

sortButton = new JButton("Sort");

J. Bhavatharni (71812201030) 6
searchButton = new JButton("Search");

modifyButton = new JButton("Modify");

// Add action listeners to buttons

addButton.addActionListener(this);

displayButton.addActionListener(this);

sortButton.addActionListener(this);

searchButton.addActionListener(this);

modifyButton.addActionListener(this);

// Add components to panel

panel.add(studentIdLabel);

panel.add(studentIdField);

panel.add(firstNameLabel);

panel.add(firstNameField);

panel.add(lastNameLabel);

panel.add(lastNameField);

panel.add(majorLabel);

panel.add(majorField);

panel.add(phoneLabel);

panel.add(phoneField);

panel.add(gpaLabel);

panel.add(gpaField);

panel.add(dobLabel);

panel.add(dobField);

J. Bhavatharni (71812201030) 7
panel.add(addButton);

panel.add(displayButton);

panel.add(sortButton);

panel.add(searchButton);

panel.add(modifyButton);

// Add panel to frame

frame.add(panel);

frame.pack();

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

@Override

public void actionPerformed(ActionEvent e) {

dbConnect db = new dbConnect();

Connection conn;

try {

conn = db.getConnection();

} catch (SQLException | ClassNotFoundException ex) {

throw new RuntimeException(ex);

try {

stmt = conn.createStatement();

J. Bhavatharni (71812201030) 8
} catch (SQLException e1) {

e1.printStackTrace();

Table tb = new Table();

if (e.getSource() == addButton) {

// Insert new student into database

String sql = "INSERT INTO sdata VALUES('" + studentIdField.getText() + "', '"

+ firstNameField.getText() + "', '" + lastNameField.getText() + "', '" +


majorField.getText()

+ "', '" + phoneField.getText() + "', '" + gpaField.getText() + "', '" +


dobField.getText() + "')";

try {

stmt.executeUpdate(sql);

JOptionPane.showMessageDialog(null, "Student added successfully.");

} catch (SQLException ex) {

ex.printStackTrace();

} else if (e.getSource() == displayButton) {

// Display all sdata in database

String sql = "SELECT * FROM sdata";

try {

ResultSet rs = stmt.executeQuery(sql);

// Create table to display student data

JTable table = new JTable(tb.buildTableModel(rs));

J. Bhavatharni (71812201030) 9
JOptionPane.showMessageDialog(null, new JScrollPane(table));

} catch (SQLException ex) {

ex.printStackTrace();

} else if (e.getSource() == sortButton) {

// Sort sdata by selected column

String[] options = {"First Name", "Last Name", "Major"};

int choice = JOptionPane.showOptionDialog(null, "Sort by:", "Sort",


JOptionPane.DEFAULT_OPTION,

JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

String sql = "";

switch (choice) {

case 0 -> sql = "SELECT * FROM sdata ORDER BY first_name";

case 1 -> sql = "SELECT * FROM sdata ORDER BY last_name";

case 2 -> sql = "SELECT * FROM sdata ORDER BY major";

default -> {

try {

ResultSet rs = stmt.executeQuery(sql);

// Create table to display student data

JTable table = new JTable(tb.buildTableModel(rs));

JOptionPane.showMessageDialog(null, new JScrollPane(table));

} catch (SQLException ex) {

J. Bhavatharni (71812201030) 10
ex.printStackTrace();

} else if (e.getSource() == searchButton) {

// Search for student by selected column

String[] options = {"Student ID", "Last Name", "Major"};

int choice = JOptionPane.showOptionDialog(null, "Search by:", "Search",


JOptionPane.DEFAULT_OPTION,

JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

String column = "";

switch (choice) {

case 0 -> column = "student_id";

case 1 -> column = "last_name";

case 2 -> column = "major";

default -> {

String searchTerm = JOptionPane.showInputDialog("Enter search term:");

String sql = "SELECT * FROM sdata WHERE " + column + " LIKE '%" +
searchTerm + "%'";

try {

ResultSet rs = stmt.executeQuery(sql);

J. Bhavatharni (71812201030) 11
// Create table to display student data

JTable table = new JTable(tb.buildTableModel(rs));

JOptionPane.showMessageDialog(null, new JScrollPane(table));

} catch (SQLException ex) {

ex.printStackTrace();

} else if (e.getSource() == modifyButton) {

// Modify selected student's data

String studentId = JOptionPane.showInputDialog("Enter student ID:");

String sql = "SELECT * FROM sdata WHERE student_id = '" + studentId + "'";

try {

ResultSet rs = stmt.executeQuery(sql);

if (rs.next()) {

String[] options = {"First Name", "Last Name", "Major", "Phone", "GPA", "Date
of Birth"};

int choice = JOptionPane.showOptionDialog(null, "Select field to modify:",


"Modify",

JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,


options, options[0]);

String column = "";

switch (choice) {

case 0 -> column = "first_name";

case 1 -> column = "last_name";

J. Bhavatharni (71812201030) 12
case 2 -> column = "major";

case 3 -> column = "phone";

case 4 -> column = "gpa";

case 5 -> column = "date_of_birth";

default -> {

String newValue = JOptionPane.showInputDialog("Enter new value:");

sql = "UPDATE sdata SET " + column + " = '" + newValue + "' WHERE
student_id = '" + studentId + "'";

stmt.executeUpdate(sql);

JOptionPane.showMessageDialog(null, "Student data updated successfully.");

} else {

JOptionPane.showMessageDialog(null, "Student not found.");

} catch (SQLException ex) {

ex.printStackTrace();

Table.java

import java.sql.*;

J. Bhavatharni (71812201030) 13
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

public class Table {

DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {

ResultSetMetaData metaData = rs.getMetaData();

// Get column names

int columnCount = metaData.getColumnCount();

Vector<String> columnNames = new Vector<String>();

for (int i = 1; i <= columnCount; i++) {

columnNames.add(metaData.getColumnName(i));

// Get row data

Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();

while (rs.next()) {

Vector<Object> row = new Vector<Object>();

for (int i = 1; i <= columnCount; i++) {

row.add(rs.getObject(i));

rowData.add(row);

return new DefaultTableModel(rowData, columnNames);

J. Bhavatharni (71812201030) 14
}

Main.java

import javax.swing.*;

public class Main {

public static void main(String[] args) {

AppGUI window = new AppGUI();

window.pack();

window.setVisible(true);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

dbConnect.java

import java.sql.*;

public class dbConnect {

private static Connection mycon=null;

public Connection getConnection() throws ClassNotFoundException, SQLException


{

String db="studata"; // replace with your database name

String user = "root", pass = <your password comes here>;

String url = "jdbc:mysql://localhost:3306/"+db;

Class.forName("com.mysql.cj.jdbc.Driver");

Connection conn = DriverManager.getConnection(url,user,pass);

return conn;

J. Bhavatharni (71812201030) 15
}

SQL Query

CREATE TABLE students (

Student_ID VARCHAR(10),

first_name VARCHAR(50),

last_name VARCHAR(50),

major VARCHAR(50),

Phone VARCHAR(15),

GPA DECIMAL(3,1),

DOB DATE

);

INSERT INTO students (Student_ID, first_name, last_name, major, Phone, GPA,


DOB)

VALUES

('001', 'Sankar', 'Nagarapu', 'EEE', '9398555299', 7.0, '2002-06-24'),

('002', 'Dharani', 'Nagarapu', 'CSE', '9133401412', 9.0, '2004-06-05'),

('003', 'Sindhu', 'Makarapu', 'CSE', '9133505025', 10.0, '2004-06-25'),

('004', 'Sneha', 'kurma', 'CSE', '9548625756', 10.0, '2004-06-27'),

('005', 'meghana', NULL, 'CSE', '9548651652', 10.0, '2004-05-22');

J. Bhavatharni (71812201030) 16
OUTPUT:

J. Bhavatharni (71812201030) 17
RESULT:
Thus, we have successfully implemented the JDBC connectivity for the application of
Student information management System.

J. Bhavatharni (71812201030) 18

You might also like