-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookDetails.java
More file actions
161 lines (121 loc) · 4.98 KB
/
Copy pathBookDetails.java
File metadata and controls
161 lines (121 loc) · 4.98 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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 BookDetails {
private Scanner sc = new Scanner(System.in);
// Insert new book
void insert(Connection connection) throws SQLException {
String query = """
INSERT INTO book
(bookid, title, publication_year, category, copies, isbn)
VALUES (?, ?, ?, ?, ?, ?)
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Book ID: ");
int bookId = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Title: ");
String title = sc.nextLine();
System.out.print("Enter Publication Year: ");
int publicationYear = sc.nextInt();
sc.nextLine();
System.out.print("Enter Category: ");
String category = sc.nextLine();
System.out.print("Enter Copies: ");
int copies = sc.nextInt();
sc.nextLine();
System.out.print("Enter ISBN: ");
String isbn = sc.nextLine();
prestmt.setInt(1, bookId);
prestmt.setString(2, title);
prestmt.setInt(3, publicationYear);
prestmt.setString(4, category);
prestmt.setInt(5, copies);
prestmt.setString(6, isbn);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✅ Book Inserted Successfully!");
} else {
System.out.println("❌ Book Insert Failed!");
}
}
}
// Delete book
void delete(Connection connection) throws SQLException {
String query = "DELETE FROM book WHERE bookid = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Book ID to Delete: ");
int bookId = sc.nextInt();
prestmt.setInt(1, bookId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("🗑 Book Deleted Successfully!");
} else {
System.out.println("❌ Book ID Not Found!");
}
}
}
// Update book details
void update(Connection connection) throws SQLException {
String query = """
UPDATE book
SET title = ?, publication_year = ?, category = ?, copies = ?, isbn = ?
WHERE bookid = ?
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Book ID to Update: ");
int bookId = sc.nextInt();
sc.nextLine();
System.out.print("Enter New Title: ");
String title = sc.nextLine();
System.out.print("Enter New Publication Year: ");
int publicationYear = sc.nextInt();
sc.nextLine();
System.out.print("Enter New Category: ");
String category = sc.nextLine();
System.out.print("Enter New Copies: ");
int copies = sc.nextInt();
sc.nextLine();
System.out.print("Enter New ISBN: ");
String isbn = sc.nextLine();
prestmt.setString(1, title);
prestmt.setInt(2, publicationYear);
prestmt.setString(3, category);
prestmt.setInt(4, copies);
prestmt.setString(5, isbn);
prestmt.setInt(6, bookId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✏ Book Updated Successfully!");
} else {
System.out.println("❌ Book ID Not Found!");
}
}
}
// Get all books
void getAll(Connection connection) throws SQLException {
String query = "SELECT * FROM book";
try (PreparedStatement prestmt = connection.prepareStatement(query);
ResultSet rs = prestmt.executeQuery()) {
System.out.println("\n===== 📚 Book List =====");
boolean found = false;
while (rs.next()) {
found = true;
System.out.println(
"Book ID: " + rs.getInt("bookid") +
" | Title: " + rs.getString("title") +
" | Year: " + rs.getInt("publication_year") +
" | Category: " + rs.getString("category") +
" | Copies: " + rs.getInt("copies") +
" | ISBN: " + rs.getString("isbn")
);
}
if (!found) {
System.out.println("No books found.");
}
}
}
}