This is a simple Java-based application to manage a collection of books in a library. The application provides a console interface for adding, viewing, updating, deleting, and searching for books. It uses MySQL for storing book data.
- Add a Book: Add a new book to the database with a title, author, and publication year.
- View All Books: Display a list of all books in the database.
- Update a Book: Update the details of an existing book by its ID.
- Delete a Book: Remove a book from the database by its ID.
- Search for a Book: Search for books by title or author (case-insensitive).
- Java Development Kit (JDK) 8 or higher
- MySQL Database
- MySQL Connector/J library
-
Install MySQL and create a database named
library
. -
Create a table named
books
with the following structure:CREATE TABLE books ( id INT PRIMARY KEY, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, year INT NOT NULL );
-
Set the database connection credentials as environment variables:
-
For Windows:
Temporarily (for the current Command Prompt session):
set DB_USER=root set DB_PASSWORD=your_password
Permanently:
- Open the Start Menu and search for "Environment Variables" and select "Edit the system environment variables."
- Click on the Environment Variables button.
- In the User variables section, click on New....
- Enter
DB_USER
as the variable name androot
as the value. - Click OK.
- Repeat the process for
DB_PASSWORD
, entering your MySQL password as the value.
-
For macOS and Linux:
Temporarily (for the current terminal session):
export DB_USER=root export DB_PASSWORD=your_password
Permanently:
-
Open your terminal.
-
Edit your shell configuration file (
~/.bashrc
,~/.bash_profile
,~/.zshrc
, etc.), depending on the shell you are using. For example:nano ~/.bashrc
-
Add the following lines to the file:
export DB_USER=root export DB_PASSWORD=your_password
-
Save the file and exit the editor.
-
Apply the changes by running:
source ~/.bashrc
-
-
-
Clone the repository:
git clone https://github.com/Pavith19/BookManagementSystem.git cd BookManagementSystem
-
Compile and run the application:
javac -cp "path/to/mysql-connector-java.jar" BookManagementSystem.java java -cp ".;path/to/mysql-connector-java.jar" BookManagementSystem
Replace
path/to/mysql-connector-java.jar
with the actual path to the MySQL Connector/J library on your system.
- Run the application and follow the on-screen menu to manage the books in your library.
- Choose an option by entering the corresponding number and pressing Enter.
The main methods in the BookManagementSystem
class include:
addBook()
: Adds a new book to the database.viewAllBooks()
: Displays all books in the database.updateBook()
: Updates an existing book's details.deleteBook()
: Deletes a book from the database.searchBooks()
: Searches for books by title or author.getYearInput()
: Gets a valid year input from the user.getAvailableBookId()
: Gets the next available book ID.printResults(ResultSet rs)
: Prints the results of a query.
MySQL, Java Programming & OOP