-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssueService.java
More file actions
58 lines (51 loc) · 2.37 KB
/
Copy pathIssueService.java
File metadata and controls
58 lines (51 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package services;
import database.Database;
import java.sql.Connection
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class IssueService {
public static void issueBook(int bookId, String issuedTo) {
String checkAvailability = "SELECT available FROM books WHERE id = ?";
String issueSql = "INSERT INTO issued_books (book_id, issued_to) VALUES (?, ?)";
String updateBook = "UPDATE books SET available = 0 WHERE id = ?";
try (Connection conn = Database.connect();
PreparedStatement checkStmt = conn.prepareStatement(checkAvailability)) {
checkStmt.setInt(1, bookId);
ResultSet rs = checkStmt.executeQuery();
if (rs.next() && rs.getBoolean("available")) {
try (PreparedStatement issueStmt = conn.prepareStatement(issueSql);
PreparedStatement updateStmt = conn.prepareStatement(updateBook)) {
issueStmt.setInt(1, bookId);
issueStmt.setString(2, issuedTo);
issueStmt.executeUpdate();
updateStmt.setInt(1, bookId);
updateStmt.executeUpdate();
System.out.println("Book issued successfully.");
}
} else {
System.out.println("Book is not available.");
}
} catch (SQLException e) {
System.out.println("Error issuing book: " + e.getMessage());
}
}
public static void returnBook(int bookId) {
String deleteIssued = "DELETE FROM issued_books WHERE book_id = ?";
String updateBook = "UPDATE books SET available = 1 WHERE id = ?";
try (Connection conn = Database.connect();
PreparedStatement deleteStmt = conn.prepareStatement(deleteIssued);
PreparedStatement updateStmt = conn.prepareStatement(updateBook)) {
deleteStmt.setInt(1, bookId);
int rowsAffected = deleteStmt.executeUpdate();
if (rowsAffected > 0) {
updateStmt.setInt(1, bookId);
updateStmt.executeUpdate();
System.out.println("Book returned successfully.");
} else {
System.out.println("Book ID not found.");
}
} catch (SQLException e) {
System.out.println("Error returning book: " + e.getMessage());
}
}
}