Ajp source code and output
Ajp source code and output
Step 1: DatabaseConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
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;
}
Step 3: LibrarianSection.java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public LibraryManager() {
this.conn = DatabaseConnection.connect();
}
// 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;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
public MainFrame() {
libraryManager = new LibraryManager();
// 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());