Java Cat 2
Java Cat 2
PROJECT TITLE:
"LIBRARY MANAGEMENT SYSTEM"
SOURCE CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.Random;
frame.add(loginButton);
frame.add(signupButton);
frame.add(exitButton);
frame.setVisible(true);
}
Object[] fields = {
"Username:", usernameField,
"Password:", passwordField
};
if (option == JOptionPane.OK_OPTION) {
String username = usernameField.getText().trim();
String password = String.valueOf(passwordField.getPassword()).trim();
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Fields cannot be empty!");
return;
}
try {
String query = "SELECT * FROM users WHERE username = ?";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String storedHash = rs.getString("password_hash");
String role = rs.getString("role");
if (storedHash.equals(hashPassword(password))) {
currentUsername = username;
currentRole = role;
JOptionPane.showMessageDialog(frame, "Login Successful!");
showLibraryDashboard();
} else {
JOptionPane.showMessageDialog(frame, "Incorrect password!");
}
} else {
JOptionPane.showMessageDialog(frame, "User not found!");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(frame, "Database error: " +
ex.getMessage());
}
}
}
Object[] fields = {
"Username:", usernameField,
"Password:", passwordField,
"Role:", roleBox
};
if (option == JOptionPane.OK_OPTION) {
String username = usernameField.getText().trim();
String password = String.valueOf(passwordField.getPassword()).trim();
String role = (String) roleBox.getSelectedItem();
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Fields cannot be empty!");
return;
}
try {
String checkQuery = "SELECT * FROM users WHERE username = ?";
PreparedStatement checkStmt =
connection.prepareStatement(checkQuery);
checkStmt.setString(1, username);
ResultSet rs = checkStmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(frame, "Username already
exists!");
return;
}
String query = "INSERT INTO users (username, password_hash, role)
VALUES (?, ?, ?)";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, hashPassword(password));
stmt.setString(3, role);
stmt.executeUpdate();
loadBooks();
frame.revalidate();
frame.repaint();
}
while (rs.next()) {
bookListModel.addElement(new Book(
rs.getString("id"),
rs.getString("title"),
rs.getString("author"),
rs.getInt("available_copies"),
rs.getInt("total_copies")));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(frame, "Error loading books: " +
e.getMessage());
}
}
try {
String query = "INSERT INTO borrowed_books (user_id, book_id,
borrow_date, due_date) VALUES ((SELECT id FROM users WHERE username
= ?), ?, ?, ?)";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString(1, currentUsername);
stmt.setString(2, selected.id);
stmt.setDate(3, java.sql.Date.valueOf(LocalDate.now()));
stmt.setDate(4, java.sql.Date.valueOf(LocalDate.now().plusDays(14)));
stmt.executeUpdate();
if (rows == 0) {
JOptionPane.showMessageDialog(frame, "You have not borrowed this
book!");
return;
}
Object[] fields = {
"Title:", titleField,
"Author:", authorField,
"Copies:", copiesField
};
if (option == JOptionPane.OK_OPTION) {
String title = titleField.getText().trim();
String author = authorField.getText().trim();
int copies;
try {
copies = Integer.parseInt(copiesField.getText().trim());
if (copies <= 0)
throw new Exception();
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Invalid number of copies!");
return;
}
try {
String bookId = generateBookId();
String query = "INSERT INTO books (id, title, author, available_copies,
total_copies) VALUES (?, ?, ?, ?, ?)";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setString(1, bookId);
stmt.setString(2, title);
stmt.setString(3, author);
stmt.setInt(4, copies);
stmt.setInt(5, copies);
stmt.executeUpdate();
try {
String deleteQuery = "DELETE FROM books WHERE id = ?";
PreparedStatement deleteStmt =
connection.prepareStatement(deleteQuery);
deleteStmt.setString(1, selected.id);
deleteStmt.executeUpdate();
if (borrowedList.length() == 0) {
JOptionPane.showMessageDialog(frame, "You have not borrowed any
books!");
} else {
JTextArea textArea = new JTextArea(borrowedList.toString());
textArea.setEditable(false);
JOptionPane.showMessageDialog(frame, new JScrollPane(textArea),
"Your Borrowed Books",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(frame, "Error fetching borrowed
books: " + e.getMessage());
}
}
OUTPUT: