Skip to main content

How to Build a Library Management System in Java

 Java Project: Library Management System



Introduction

Library Management System (LMS) is a software application that helps libraries manage books, users, and transactions efficiently. In this project, we will develop a Java-based Library Management System using Java, Swing (for GUI), and MySQL (for database storage).

This project will allow users (students/librarians) to:

  • Add, remove, and search for books

  • Register as library members

  • Borrow and return books

  • Maintain a database of books and users


Project Features

1. User Management

  • Users can register with their name and contact details.

  • The system will allow librarians and students to log in.

  • Librarians will have full access to add, remove, and update books.

  • Students can only borrow or return books.

2. Book Management

  • Add, remove, update, and search for books.

  • Books will have ISBN numbers, titles, authors, genres, and availability status.

3. Borrow & Return Books

  • Users can borrow books, and the system will update the book status.

  • Due dates will be assigned for borrowed books.

  • Books must be returned before the due date, or a fine may be imposed.

4. Database Storage

  • All information (users, books, transactions) will be stored in MySQL.

  • JDBC will be used to connect Java with the database.


Technology Stack

Frontend (GUI)

  • Java Swing (for designing the user interface)

  • JFrame, JPanel, JTextField, JButton, JTable

Backend (Logic & Database)

  • Java (Core Java, OOP concepts, Exception Handling)

  • MySQL (Database storage)

  • JDBC (Java Database Connectivity) for database connection


Project Setup

1. Install Required Software

Before we start coding, make sure you have:
✅ JDK (Java Development Kit) installed
✅ MySQL installed
✅ Eclipse/IntelliJ as the IDE
✅ MySQL JDBC Driver downloaded


Step 1: Database Setup

First, create a MySQL database for storing book and user details.

SQL Commands to Create Database & Tables

sql
CREATE DATABASE LibraryDB;
 USE LibraryDB; 
CREATE TABLE Users (
 user_id INT AUTO_INCREMENT PRIMARY KEY, 
 name VARCHAR(100), 
 email VARCHAR(100) UNIQUE
 password VARCHAR(50),
 user_type ENUM('librarian', 'student'
); CREATE TABLE Books ( 
 book_id INT AUTO_INCREMENT PRIMARY KEY,
 title VARCHAR(200),
 author VARCHAR(200), 
 isbn VARCHAR(50) UNIQUE,
 genre VARCHAR(50), 
 status ENUM('available', 'borrowed') DEFAULT 'available'
); 
CREATE TABLE Transactions (
 transaction_id INT AUTO_INCREMENT PRIMARY KEY,
 user_id INT
 book_id INT,
 borrow_date DATE,
 return_date DATE,
FOREIGN KEY (user_id) REFERENCES Users(user_id), 
FOREIGN KEY (book_id) REFERENCES Books(book_id) 
);

Step 2: Java Project Structure

📂 LibraryManagementSystem/
  📂 src/
    📂 dao/ (Database connection logic)
    📂 models/ (User, Book, Transaction classes)
    📂 views/ (Swing GUI Forms)
    📂 controllers/ (Business logic)
    📜 Main.java (Program entry point)


Step 3: Create Database Connection (JDBC)

Create a class DatabaseConnection.java to handle MySQL connectivity.

java
package dao; import java.sql.*;
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/LibraryDB";
private static final String USER = "root";
private static final String PASSWORD = "yourpassword";
public static Connection getConnection()
try
 Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(URL, USER, PASSWORD);
 } catch (Exception e) {
 e.printStackTrace(); return null; } 
 } }

Step 4: Create User Model

java
package models; 
public class User
private int userId;
private String name; 
private String email;
private String password;
private String userType;
public User(
int userId, 
String name,
String email, 
String password,
 String userType) {
this.userId = userId; 
this.name = name; 
this.email = email; 
this.password = password;
this.userType = userType;
 } // Getters and Setters }

Step 5: Create User Authentication

java
package controllers;
import dao.DatabaseConnection; 
import models.User; 
import java.sql.Connection; 
import java.sql.PreparedStatement;
import java.sql.ResultSet; 
public class UserController
public static User loginUser(String email, String password) {
Connection conn = DatabaseConnection.getConnection(); 
String query = "SELECT * FROM Users WHERE email = ? AND password = ?"
try {
PreparedStatement ps = conn.prepareStatement(query);
 ps.setString(1, email);
 ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return new User(rs.getInt("user_id"),
 rs.getString("name"),
 rs.getString("email"), 
rs.getString("password"),
 rs.getString("user_type"));
 } 
 } catch (Exception e) { e.printStackTrace();
 } 
return null; } 
}

Step 6: Create Swing GUI for Login

java
package views;
import controllers.UserController; 
import models.User; 
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView { public static void main(String[] args) { JFrame frame = new JFrame("Library Login");
 frame.setSize(400, 300);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
 frame.add(panel);
 placeComponents(panel); 
 frame.setVisible(true);
 } private static void placeComponents(JPanel panel) { panel.setLayout(null);
JLabel userLabel = new JLabel("Email:");
 userLabel.setBounds(10, 20, 80, 25); 
 panel.add(userLabel);
JTextField userText = new JTextField(20);
 userText.setBounds(100, 20, 165, 25);
 panel.add(userText);
JLabel passwordLabel = new JLabel("Password:");
 passwordLabel.setBounds(10, 50, 80, 25);
 panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
 passwordText.setBounds(100, 50, 165, 25);
 panel.add(passwordText); 
JButton loginButton = new JButton("Login");
 loginButton.setBounds(10, 80, 80, 25);
 panel.add(loginButton);
 loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String email = userText.getText();
String password = new String(passwordText.getPassword());
User user = UserController.loginUser(email, password);
if (user != null) {
 JOptionPane.showMessageDialog(null, "Login Successful! Welcome " + user.getName());
 }
else { JOptionPane.showMessageDialog(null, "Invalid Credentials"); 
 } }
 });
 } }

Conclusion

In this project, we created a Library Management System using Java, Swing, and MySQL. We covered:
✅ Setting up a MySQL database
✅ Connecting Java with MySQL using JDBC
✅ Creating models for users and books
✅ Implementing user authentication with a Swing GUI