-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.java
More file actions
130 lines (95 loc) · 4.17 KB
/
Copy pathTransaction.java
File metadata and controls
130 lines (95 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
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 Transaction {
private Scanner sc = new Scanner(System.in);
// Insert Transaction
void insert(Connection connection) throws SQLException {
String query = """
INSERT INTO transactions
(transaction_id, member_id, book_id, issue_date, return_date)
VALUES (?, ?, ?, ?, ?)
""";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Transaction ID: ");
int transactionId = 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();
System.out.print("Enter Return Date (YYYY-MM-DD): ");
String returnDate = sc.next();
prestmt.setInt(1, transactionId);
prestmt.setInt(2, memberId);
prestmt.setInt(3, bookId);
prestmt.setString(4, issueDate);
prestmt.setString(5, returnDate);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✅ Transaction Inserted Successfully!");
} else {
System.out.println("❌ Transaction Insert Failed!");
}
}
}
// Delete Transaction
void delete(Connection connection) throws SQLException {
String query = "DELETE FROM transactions WHERE transaction_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Transaction ID to Delete: ");
int transactionId = sc.nextInt();
prestmt.setInt(1, transactionId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("🗑 Transaction Deleted Successfully!");
} else {
System.out.println("❌ Transaction ID Not Found!");
}
}
}
// Update Transaction
void update(Connection connection) throws SQLException {
String query = "UPDATE transactions SET return_date = ? WHERE transaction_id = ?";
try (PreparedStatement prestmt = connection.prepareStatement(query)) {
System.out.print("Enter Transaction ID to Update: ");
int transactionId = sc.nextInt();
System.out.print("Enter New Return Date (YYYY-MM-DD): ");
String returnDate = sc.next();
prestmt.setString(1, returnDate);
prestmt.setInt(2, transactionId);
int rows = prestmt.executeUpdate();
if (rows > 0) {
System.out.println("✏ Transaction Updated Successfully!");
} else {
System.out.println("❌ Transaction ID Not Found!");
}
}
}
// Get All Transactions
void getAll(Connection connection) throws SQLException {
String query = "SELECT * FROM transactions";
try (PreparedStatement prestmt = connection.prepareStatement(query);
ResultSet rs = prestmt.executeQuery()) {
System.out.println("\n===== 📑 Transactions List =====");
boolean found = false;
while (rs.next()) {
found = true;
System.out.println(
"Transaction ID: " + rs.getInt("transaction_id") +
" | Member ID: " + rs.getInt("member_id") +
" | Book ID: " + rs.getInt("book_id") +
" | Issue Date: " + rs.getDate("issue_date") +
" | Return Date: " + rs.getDate("return_date")
);
}
if (!found) {
System.out.println("No transactions found.");
}
}
}
}