Case Study: Developing a Java Swing Application with MySQL Database
Integration
Overview
This case study outlines the development of a Java Swing application integrated with a MySQL
database to manage a simple Library Management System (LMS). The system allows users to
perform CRUD (Create, Read, Update, Delete) operations on books and member records.
Problem Statement
Libraries often require efficient systems to manage their inventory and member data. Manual or
spreadsheet-based management systems are prone to errors and inefficiencies. The goal of this
project is to develop a desktop application using Java Swing for the user interface and MySQL
for data storage.
System Features
1. Book Management: Add, edit, delete, and view books in the library.
2. Member Management: Maintain records of library members, including their borrowing
history.
3. Issue/Return Books: Track borrowed books and manage due dates.
4. Search Functionality: Quickly search for books and members using keywords.
5. Login System: Authenticate users with role-based access control (admin/staff).
System Architecture
1. Front-End
Java Swing: Provides a graphical user interface (GUI) for the application, including
forms, tables, and dialog boxes.
2. Back-End
MySQL Database: Stores library data, including book details, member information, and
transaction logs.
JDBC (Java Database Connectivity): Acts as a bridge between the Java application and
the MySQL database.
Implementation Details
1. Database Design
Tables:
books: Stores details about the books (id, title, author, genre, availability).
members: Contains information about members (id, name, contact details).
transactions: Tracks issued and returned books (transaction_id, member_id, book_id,
issue_date, return_date).
users: Stores credentials for the login system (id, username, password, role).
SQL Script:
sql
Copy code
CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255),
genre VARCHAR(100),
availability BOOLEAN DEFAULT TRUE
);
CREATE TABLE members (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
contact VARCHAR(100) NOT NULL
);
CREATE TABLE transactions (
transaction_id INT AUTO_INCREMENT PRIMARY KEY,
member_id INT,
book_id INT,
issue_date DATE,
return_date DATE,
FOREIGN KEY (member_id) REFERENCES members(id),
FOREIGN KEY (book_id) REFERENCES books(id)
);
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role ENUM('admin', 'staff') DEFAULT 'staff'
);
2. Application Design
Modules:
1. Login Module: Handles user authentication.
2. Main Dashboard: Provides access to different functionalities (book management,
member management, etc.).
3. CRUD Operations: Manage books and members through forms and tables.
4. Transaction Module: Issue and return books with validation (e.g., book availability).
5. Search Module: Allows searching through lists dynamically.
Technologies Used:
Java Swing for GUI
MySQL for database
JDBC for database communication
3. Code Implementation
Database Connection Class:
java
Copy code
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
public static Connection connect() {
try {
String url = "jdbc:mysql://localhost:3306/library";
String user = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url, user,
password);
System.out.println("Database connected successfully!");
return connection;
} catch (Exception e) {
System.out.println("Error connecting to database: " +
e.getMessage());
return null;
}
}
}
Login Form:
java
Copy code
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class LoginForm extends JFrame {
JTextField usernameField;
JPasswordField passwordField;
JButton loginButton;
public LoginForm() {
setTitle("Login");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
JLabel usernameLabel = new JLabel("Username:");
usernameLabel.setBounds(20, 20, 100, 25);
add(usernameLabel);
usernameField = new JTextField();
usernameField.setBounds(120, 20, 150, 25);
add(usernameField);
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(20, 60, 100, 25);
add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setBounds(120, 60, 150, 25);
add(passwordField);
loginButton = new JButton("Login");
loginButton.setBounds(100, 100, 80, 25);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
login();
}
});
add(loginButton);
}
public void login() {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
try (Connection con = DatabaseConnection.connect()) {
String query = "SELECT * FROM users WHERE username = ? AND
password = ?";
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(this, "Login successful!");
new Dashboard().setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(this, "Invalid credentials!");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
public static void main(String[] args) {
new LoginForm().setVisible(true);
}
}
Challenges
1. Database Connection Issues: Debugging errors related to incorrect credentials or JDBC
configuration.
2. Swing GUI Management: Handling layouts and event-driven programming for dynamic
forms.
3. Concurrency: Ensuring database consistency when multiple users access the system.
Conclusion
The Java Swing application integrated with MySQL effectively addresses the library's
management needs. The modular design ensures scalability, allowing for future enhancements
such as reporting and advanced analytics. It serves as an excellent example of combining desktop
application development with relational database integration.