-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryService.java
More file actions
139 lines (103 loc) · 4.47 KB
/
Copy pathLibraryService.java
File metadata and controls
139 lines (103 loc) · 4.47 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
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 LibraryService {
private Scanner sc = new Scanner(System.in);
// Insert new Library Service
void insert(Connection connection) throws SQLException {
String query = """
INSERT INTO library_service
(service_id, service_name, description, status)
VALUES (?, ?, ?, ?)
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Service ID: ");
int serviceId = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Service Name: ");
String serviceName = sc.nextLine();
System.out.print("Enter Description: ");
String description = sc.nextLine();
System.out.print("Enter Status (Active/Inactive): ");
String status = sc.nextLine();
prestmt.setInt(1, serviceId);
prestmt.setString(2, serviceName);
prestmt.setString(3, description);
prestmt.setString(4, status);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✅ Library Service Inserted Successfully!");
} else {
System.out.println("❌ Library Service Insert Failed!");
}
}
}
// Delete Library Service
void delete(Connection connection) throws SQLException {
String query = "DELETE FROM library_service WHERE service_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Service ID to Delete: ");
int serviceId = sc.nextInt();
prestmt.setInt(1, serviceId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("🗑 Library Service Deleted Successfully!");
} else {
System.out.println("❌ Service ID Not Found!");
}
}
}
// Update Library Service
void update(Connection connection) throws SQLException {
String query = """
UPDATE library_service
SET service_name = ?, description = ?, status = ?
WHERE service_id = ?
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Service ID to Update: ");
int serviceId = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter New Service Name: ");
String serviceName = sc.nextLine();
System.out.print("Enter New Description: ");
String description = sc.nextLine();
System.out.print("Enter New Status (Active/Inactive): ");
String status = sc.nextLine();
prestmt.setString(1, serviceName);
prestmt.setString(2, description);
prestmt.setString(3, status);
prestmt.setInt(4, serviceId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✏ Library Service Updated Successfully!");
} else {
System.out.println("❌ Service ID Not Found!");
}
}
}
// Get All Library Services
void getAll(Connection connection) throws SQLException {
String query = "SELECT * FROM library_service";
try (PreparedStatement prestmt = connection.prepareStatement(query);
ResultSet rs = prestmt.executeQuery()) {
System.out.println("\n===== 📚 Library Services List =====");
boolean found = false;
while (rs.next()) {
found = true;
System.out.println(
"Service ID: " + rs.getInt("service_id") +
" | Service Name: " + rs.getString("service_name") +
" | Description: " + rs.getString("description") +
" | Status: " + rs.getString("status")
);
}
if (!found) {
System.out.println("No library services found.");
}
}
}
}