-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssueBook.java
More file actions
137 lines (101 loc) · 4.17 KB
/
Copy pathIssueBook.java
File metadata and controls
137 lines (101 loc) · 4.17 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package LMS.com;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class IssueBook {
private Scanner sc = new Scanner(System.in);
// Insert new Issue record
void insert(Connection connection) throws SQLException {
String query = """
INSERT INTO issue_book
(issue_id, member_id, book_id, issue_date)
VALUES (?, ?, ?, ?)
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Issue ID: ");
int issueId = sc.nextInt();
System.out.print("Enter Member ID: ");
int memberId = sc.nextInt();
System.out.print("Enter Book ID: ");
int bookId = sc.nextInt();
System.out.print("Enter Issue Date (YYYY-MM-DD): ");
String issueDate = sc.next();
prestmt.setInt(1, issueId);
prestmt.setInt(2, memberId);
prestmt.setInt(3, bookId);
prestmt.setString(4, issueDate);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✅ Book Issued Successfully!");
} else {
System.out.println("❌ Issue Failed!");
}
}
}
// Delete Issue record
void delete(Connection connection) throws SQLException {
String query = "DELETE FROM issue_book WHERE issue_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Issue ID to Delete: ");
int issueId = sc.nextInt();
prestmt.setInt(1, issueId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("🗑 Issue Record Deleted Successfully!");
} else {
System.out.println("❌ Issue ID Not Found!");
}
}
}
// Update Issue record
void update(Connection connection) throws SQLException {
String query = """
UPDATE issue_book
SET member_id = ?, book_id = ?, issue_date = ?
WHERE issue_id = ?
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Issue ID to Update: ");
int issueId = sc.nextInt();
System.out.print("Enter New Member ID: ");
int memberId = sc.nextInt();
System.out.print("Enter New Book ID: ");
int bookId = sc.nextInt();
System.out.print("Enter New Issue Date (YYYY-MM-DD): ");
String issueDate = sc.next();
prestmt.setInt(1, memberId);
prestmt.setInt(2, bookId);
prestmt.setString(3, issueDate);
prestmt.setInt(4, issueId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✏ Issue Record Updated Successfully!");
} else {
System.out.println("❌ Issue ID Not Found!");
}
}
}
// Get all Issue records
void getAll(Connection connection) throws SQLException {
String query = "SELECT * FROM issue_book";
try (PreparedStatement prestmt = connection.prepareStatement(query);
ResultSet rs = prestmt.executeQuery()) {
System.out.println("\n===== 📚 Issued Books =====");
boolean found = false;
while (rs.next()) {
found = true;
System.out.println(
"Issue ID: " + rs.getInt("issue_id") +
" | Member ID: " + rs.getInt("member_id") +
" | Book ID: " + rs.getInt("book_id") +
" | Issue Date: " + rs.getDate("issue_date")
);
}
if (!found) {
System.out.println("No issued books found.");
}
}
}
}