0% found this document useful (0 votes)
1 views

Library Management System

The Library Management System is a full-stack web application designed to streamline library operations, enabling users to manage books, authors, and borrowers through a user-friendly interface. It utilizes a Spring Boot RESTful backend for CRUD operations and includes various software tools such as Java, H2 database, and Hibernate for effective data management. The application features REST APIs for backend communication and a frontend built with HTML, CSS, and JavaScript for an intuitive user experience.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Library Management System

The Library Management System is a full-stack web application designed to streamline library operations, enabling users to manage books, authors, and borrowers through a user-friendly interface. It utilizes a Spring Boot RESTful backend for CRUD operations and includes various software tools such as Java, H2 database, and Hibernate for effective data management. The application features REST APIs for backend communication and a frontend built with HTML, CSS, and JavaScript for an intuitive user experience.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Library

Management
System
Problem Statement
The Library Managment System is a full-stack web application developed to simplify the management of
library eperations as managing books, authors, and borrower. It allows users to perform CRUD operations
and interaction through a clean, intuitive frontend powered by a Spring Boot RESTful backend.

Objectives
1.) Develop a robust and scalable library management application,

2) Allow users to manage (create, read, update, delete) books & borrower details efficiently.

3.) Implement REST APIs for backend operations,

4.) Improve frontend-backend communication through a clean user interface.

Required Software Tools


Tool purpose
1.Springboot For backend development

2.Java 21 GUI library

3.H2 database Lightweight database for storing inventory data

4.IDE Writing and editing code

5.Frontend used for HTML,CSS,JAVASCRIPT

6.RESTFUL API used for fetching the data form database

7.Hibernate mapping the database into code


CODING PARTS
1.src/main/java – package:com.example.library

package com.example.library;

import com.example.library.model.Book;

import com.example.library.service.BookService;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

public class LibraryApplication {

public static void main(String[] args) {

SpringApplication.run(LibraryApplication.class, args);

// This method will be called to initialize the database with sample books

@Bean

CommandLineRunner initDatabase(BookService service) {

return args -> {

// Adding initial sample data to the book database

service.addBook(new Book("Introduction to Algorithms", "Thomas H. Cormen"));

service.addBook(new Book("Clean Code", "Robert C. Martin"));

service.addBook(new Book("Design Patterns", "Erich Gamma"));

service.addBook(new Book("Operating System Concepts", "Abraham Silberschatz"));

service.addBook(new Book("Database System Concepts", "Henry F. Korth"));


service.addBook(new Book("Artificial Intelligence: A Modern Approach", "Stuart Russell"));

service.addBook(new Book("Computer Networks", "Andrew S. Tanenbaum"));

service.addBook(new Book("Modern Operating Systems", "Andrew S. Tanenbaum"));

service.addBook(new Book("The Pragmatic Programmer", "Andrew Hunt"));

service.addBook(new Book("Structure and Interpretation of Computer Programs", "Harold


Abelson"));

service.addBook(new Book("Compilers: Principles, Techniques, and Tools", "Alfred V. Aho"));

service.addBook(new Book("Computer Architecture", "John L. Hennessy"));

service.addBook(new Book("Introduction to the Theory of Computation", "Michael Sipser"));

service.addBook(new Book("The Art of Computer Programming", "Donald E. Knuth"));

service.addBook(new Book("Java: The Complete Reference", "Herbert Schildt"));

service.addBook(new Book("Effective Java", "Joshua Bloch"));

service.addBook(new Book("Python Crash Course", "Eric Matthes"));

service.addBook(new Book("You Don't Know JS", "Kyle Simpson"));

service.addBook(new Book("Head First Design Patterns", "Eric Freeman"));

service.addBook(new Book("Code: The Hidden Language of Computer Hardware and Software",


"Charles Petzold"));

};

2.Package:com.example.library.controller

package com.example.library.controller;

import com.example.library.model.Book;

import com.example.library.service.BookService;

import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.Optional;
@RestController

@RequestMapping("/api/books")

@CrossOrigin

public class BookController {

private final BookService service;

public BookController(BookService service) {

this.service = service;

@GetMapping

public List<Book> getAllBooks() {

return service.getAllBooks();

@GetMapping("/{id}")

public Optional<Book> getBookById(@PathVariable Long id) {

return service.getBookById(id);

@PostMapping

public Book addBook(@RequestBody Book book) {

return service.addBook(book);

@DeleteMapping("/{id}")

public void deleteBook(@PathVariable Long id) {


service.deleteBook(id);

@PutMapping("/{id}")

public Book updateBook(@PathVariable Long id, @RequestBody Book book) {

return service.updateBook(id, book);

3.Package:com.example.library.model

package com.example.library.model;

public class Book {

private Long id;

private String title;

private String author;

private boolean available = true;

public Book() {

public Book(String title, String author) {

this.title = title;

this.author = author;

public Book(Long id, String title, String author, boolean available) {

this.id = id;

this.title = title;

this.author = author;

this.available = available;

}
public Long getId() { return id; }

public void setId(Long id) { this.id = id; }

public String getTitle() { return title; }

public void setTitle(String title) { this.title = title; }

public String getAuthor() { return author; }

public void setAuthor(String author) { this.author = author; }

public boolean isAvailable() { return available; }

public void setAvailable(boolean available) { this.available = available; }

4.package:com.example.library.service

import com.example.library.model.Book;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Optional;

@Service

public class BookService {

private final JdbcTemplate jdbcTemplate;

public BookService(JdbcTemplate jdbcTemplate) {


this.jdbcTemplate = jdbcTemplate;

public List<Book> getAllBooks() {

return jdbcTemplate.query("SELECT * FROM book", new BeanPropertyRowMapper<>(Book.class));

public Optional<Book> getBookById(Long id) {

List<Book> books = jdbcTemplate.query("SELECT * FROM book WHERE id = ?", new


BeanPropertyRowMapper<>(Book.class), id);

return books.stream().findFirst();

public Book addBook(Book book) {

jdbcTemplate.update("INSERT INTO book (title, author, available) VALUES (?, ?, ?)", book.getTitle(),
book.getAuthor(), book.isAvailable());

return book;

public void deleteBook(Long id) {

jdbcTemplate.update("DELETE FROM book WHERE id = ?", id);

public Book updateBook(Long id, Book book) {

jdbcTemplate.update("UPDATE book SET title = ?, author = ?, available = ? WHERE id = ?",


book.getTitle(), book.getAuthor(), book.isAvailable(), id);

book.setId(id);

return book;

}
}

2.src/main/resources/static

1.index.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Library Management</title>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<div class="container">

<h1>Library Management System</h1>

<h2>Books</h2>

<!-- Form to add new book -->

<div class="form-container">

<h3>Add New Book</h3>

<form id="add-book-form">

<input type="text" id="title" placeholder="Book Title" required />

<input type="text" id="author" placeholder="Book Author" required />

<button type="submit">Add Book</button>

</form>

</div>

<!-- List of books -->

<h3>Books in Library</h3>

<ul id="book-list"></ul>

</div>
<script src="script.js"></script>

</body>

</html>

2.styles.css

/* Reset some basic styles */

*{

margin: 0;

padding: 0;

box-sizing: border-box;

/* Container styles */

.container {

width: 80%;

margin: auto;

padding: 20px;

/* Title styles */

h1 {

text-align: center;

color: #333;

/* Form styles */

.form-container {

margin-bottom: 20px;

form input {

padding: 8px;
margin: 5px;

width: 200px;

font-size: 16px;

form button {

padding: 8px 16px;

background-color: #4CAF50;

color: white;

border: none;

cursor: pointer;

form button:hover {

background-color: #45a049;

/* Book list styles */

ul {

list-style-type: none;

padding: 0;

li {

padding: 10px;

background-color: #f9f9f9;

margin: 5px;

border: 1px solid #ddd;

3.script.js

// Get the form and elements


const addBookForm = document.getElementById('add-book-form');

const bookList = document.getElementById('book-list');

// API URLs

const apiUrl = 'http://localhost:8080/api/books';

// Function to fetch books from the API

async function getBooks() {

try {

const response = await fetch(apiUrl);

const books = await response.json();

displayBooks(books);

} catch (error) {

console.error('Error fetching books:', error);

// Function to display books in the list

function displayBooks(books) {

bookList.innerHTML = '';

books.forEach(book => {

const li = document.createElement('li');

li.textContent = `${book.title} by ${book.author}`;

bookList.appendChild(li);

});

// Function to handle form submission (Add Book)

addBookForm.addEventListener('submit', async (event) => {

event.preventDefault();

const title = document.getElementById('title').value;


const author = document.getElementById('author').value;

try {

const response = await fetch(apiUrl, {

method: 'POST',

headers: {

'Content-Type': 'application/json'

},

body: JSON.stringify({ title, author })

});

if (response.ok) {

getBooks(); // Refresh the list after adding

addBookForm.reset();

} else {

alert('Failed to add the book');

} catch (error) {

console.error('Error adding book:', error);

});

// Initial fetch of books

getBooks();

3.In marven project add dependencies in H2 database to the porn.xml

<dependency>

<groupId>com.h2database</groupId>

<artifactId>h2</artifactId>

<scope>runtime</scope>

</dependency>
Configuration
# H2 Database Configuration

spring.datasource.url=jdbc:h2:mem:testdb

spring.datasource.driverClassName=org.h2.Driver

spring.datasource.username=sa

spring.datasource.password=

# JPA Settings

spring.jpa.database-platform=org.hibernate.H2DB

spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

# Enable H2 Console

spring.h2.console.enabled=true

spring.h2.console.path=/h2-console

Run Application
1. Build and run the project:via cmd or console window

./mvnw spring-boot:run

or using Gradle
./gradlew bootRun

2.Access H2 Console:

o URL: http://localhost:8080/h2-console

o JDBC URL: jdbc:h2:File:testdb

o Username: sa

o Password: (leave blank)


📬 Sample REST Endpoints(REST API CONFIGURATION)

HTTP Method URL Description

GET Get all entities


/api/entities

POST /api/entities Create a new entity

/api/entities/
GET Get entity by ID
{id}

/api/entities/
PUT Update entity
{id}

/api/entities/
DELETE Delete entity
{id}

Sample output

You might also like