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

Ajp source code and output

Uploaded by

rajmalage2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Ajp source code and output

Uploaded by

rajmalage2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

6.

Project Source Code/output


 MySQL Database Setup

CREATE DATABASE LibraryDB;


USE LibraryDB;
CREATE TABLE Books (
book_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100),
author VARCHAR(100),
publisher VARCHAR(100),
status ENUM('available', 'issued') DEFAULT 'available'
);
CREATE TABLE Users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
password VARCHAR(100)
);

 Step 1: DatabaseConnection.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {


private static final String URL = "jdbc:mysql://localhost:3306/LibraryDB";
private static final String USER = "root"; // Your MySQL username
private static final String PASSWORD = "password"; // Your MySQL password

public static Connection connect() {


try {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException | ClassNotFoundException e) {
System.err.println("Connection failed: " + e.getMessage());
return null;
}
}
}
 Step 2: Book.java

public class Book {


private int bookId;
private String title;
private String author;
private String publisher;
private String status;

public Book(int bookId, String title, String author, String publisher, String status) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.publisher = publisher;
this.status = status;
}

// Getters and setters


public int getBookId() {
return bookId;
}

public void setBookId(int bookId) {


this.bookId = bookId;
}

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 String getPublisher() {


return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}

public String getStatus() {


return status;
}

public void setStatus(String status) {


this.status = status;
}
}

 Step 3: LibrarianSection.java

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LibrarianSection {


private Connection conn;

public LibraryManager() {
this.conn = DatabaseConnection.connect();
}

// Add a new book


public boolean addBook(Book book) {
String query = "INSERT INTO Books (title, author, publisher, status) VALUES (?,
?, ?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, book.getTitle());
stmt.setString(2, book.getAuthor());
stmt.setString(3, book.getPublisher());
stmt.setString(4, book.getStatus());
int result = stmt.executeUpdate();
return result > 0;
} catch (SQLException e) {
System.err.println("Error adding book: " + e.getMessage());
return false;
}
}

// Search books by title or author


public List<Book> searchBooks(String keyword) {
List<Book> books = new ArrayList<>();
String query = "SELECT * FROM Books WHERE title LIKE ? OR author LIKE ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, "%" + keyword + "%");
stmt.setString(2, "%" + keyword + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
books.add(new Book(rs.getInt("book_id"), rs.getString("title"),
rs.getString("author"), rs.getString("publisher"), rs.getString("status")));
}
} catch (SQLException e) {
System.err.println("Error searching books: " + e.getMessage());
}
return books;
}

// Issue a book
public boolean issueBook(int bookId) {
String query = "UPDATE Books SET status = 'issued' WHERE book_id = ? AND
status = 'available'";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setInt(1, bookId);
int result = stmt.executeUpdate();
return result > 0;
} catch (SQLException e) {
System.err.println("Error issuing book: " + e.getMessage());
return false;
}
}

// Return a book
public boolean returnBook(int bookId) {
String query = "UPDATE Books SET status = 'available' WHERE book_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setInt(1, bookId);
int result = stmt.executeUpdate();
return result > 0;
} catch (SQLException e) {
System.err.println("Error returning book: " + e.getMessage());
return false;
}
}

// Get all books


public List<Book> getAllBooks() {
List<Book> books = new ArrayList<>();
String query = "SELECT * FROM Books";
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
books.add(new Book(rs.getInt("book_id"), rs.getString("title"),
rs.getString("author"), rs.getString("publisher"), rs.getString("status")));
}
} catch (SQLException e) {
System.err.println("Error fetching books: " + e.getMessage());
}
return books;
}
}

 Step 4: MainFrame.java (GUI with Swing)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;

public class MainFrame extends JFrame {


private JTextField searchField;
private JTextArea resultArea;
private JButton searchButton, issueButton, returnButton, addButton;
private LibraryManager libraryManager;

public MainFrame() {
libraryManager = new LibraryManager();

setTitle("Library Management System");


setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

// Create UI Components
searchField = new JTextField(20);
resultArea = new JTextArea();
resultArea.setEditable(false);
searchButton = new JButton("Search");
issueButton = new JButton("Issue Book");
returnButton = new JButton("Return Book");
addButton = new JButton("Add Book");

// Set Layout
setLayout(new FlowLayout());
add(new JLabel("Search Books:"));
add(searchField);
add(searchButton);
add(new JScrollPane(resultArea));
add(issueButton);
add(returnButton);
add(addButton);

// Event Listeners
searchButton.addActionListener(e -> searchBooks());
issueButton.addActionListener(e -> issueBook());
returnButton.addActionListener(e -> returnBook());
addButton.addActionListener(e -> addBook());

// Display the frame


setVisible(true);
}

private void searchBooks() {


String keyword = searchField.getText();
List<Book> books = libraryManager.searchBooks(keyword);
resultArea.setText("");
for (Book book : books) {
resultArea.append(book.getTitle() + " by " + book.getAuthor() + " - " +
book.getStatus() + "\n");
}
}

private void issueBook() {


try {
int bookId = Integer.parseInt(JOptionPane.showInputDialog(this, "Enter Book ID
to Issue:"));
if (libraryManager.issueBook(bookId)) {
JOptionPane.showMessageDialog(this, "Book Issued Successfully.");
} else {
JOptionPane.showMessageDialog(this, "Book cannot be issued.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Invalid Book ID.");
}
}

private void returnBook() {


try {
int bookId = Integer.parseInt(JOptionPane.showInputDialog(this, "Enter Book ID
to Return:"));
if (libraryManager.returnBook(bookId)) {
JOptionPane.showMessageDialog(this, "Book Returned Successfully.");
} else {
JOptionPane.showMessageDialog(this, "Book cannot be returned.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Invalid Book ID.");
}
}

private void addBook() {


String title = JOptionPane.showInputDialog(this, "Enter Book Title:");
String author = JOptionPane.showInputDialog(this, "Enter Author Name:");
String publisher = JOptionPane.showInputDialog(this, "Enter Publisher Name:");
Book book = new Book(0, title, author, publisher, "available");
if (libraryManager.addBook(book)) {
JOptionPane.showMessageDialog(this, "Book Added Successfully.");
} else {
JOptionPane.showMessageDialog(this, "Error adding book.");
}
}
}

 Step 5: LibraryApp.java (Main class)

public class LibraryApp {


public static void main(String[] args) {
// Start the application
new MainFrame();
}
}

You might also like