-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurity.java
226 lines (184 loc) · 7.97 KB
/
Security.java
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package DBMS_Project_Demo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Security {
private static final Scanner scanner = new Scanner(System.in);
public static void securityOptions(Statement statement) {
try {
// Step 1: Load and register the JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
int choice;
do {
// Provide options to the user
System.out.println("Choose an option:");
System.out.println("1. Insert Security");
System.out.println("2. Update Security");
System.out.println("3. Get Security");
System.out.println("4. Delete Security");
System.out.println("5. Update payment status(Paid/Unpaid) for a specific citation");
System.out.println("6. Generate Citation");
System.out.println("7. Back to the main menu");
// Get user choice
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
// Perform the chosen action
switch (choice) {
case 1:
insertSecurity(statement);
break;
case 2:
updateSecurity(statement);
break;
case 3:
getSecurity(statement);
break;
case 4:
deleteSecurity(statement);
break;
case 5:
updatePaymentStatus(statement);
break;
case 6:
generateCitation(statement);
break;
case 7:
System.out.println("Back to Home menu");
break;
default:
System.out.println("Invalid choice. Please enter a number between 1 and 4.");
}
} while (choice != 7);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static void insertSecurity(Statement statement) {
try{
System.out.println("Enter Security ID to insert:");
int securityID = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
String insertQuery = "INSERT INTO Security (securityID) VALUES (" + securityID + ")";
int rowsAffected = statement.executeUpdate(insertQuery);
if (rowsAffected > 0) {
System.out.println("Security information inserted successfully.");
} else {
System.out.println("Failed to insert security information.");
}
} catch (SQLException e) {
handleSQLException(e);
}
}
private static void updateSecurity(Statement statement) {
try{
System.out.println("Enter Security ID to update:");
int securityID = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.println("Enter new Security ID:");
int newSecurityID = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
String updateQuery = "UPDATE Security SET securityID = " + newSecurityID + " WHERE securityID = " + securityID;
int rowsAffected = statement.executeUpdate(updateQuery);
if (rowsAffected > 0) {
System.out.println("Security information updated successfully.");
} else {
System.out.println("Security not found for update.");
}
} catch (SQLException e) {
handleSQLException(e);
}
}
private static void getSecurity(Statement statement) {
try{
String selectQuery = "SELECT * FROM Security";
try (ResultSet resultSet = statement.executeQuery(selectQuery)) {
while (resultSet.next()) {
// Process and print security information
System.out.println("Security ID: " + resultSet.getInt("securityID"));
System.out.println("------------");
}
}
} catch (SQLException e) {
handleSQLException(e);
}
}
private static void deleteSecurity(Statement statement) {
try{
System.out.println("Enter Security ID to delete:");
int securityID = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
String deleteQuery = "DELETE FROM Security WHERE securityID = " + securityID;
int rowsAffected = statement.executeUpdate(deleteQuery);
if (rowsAffected > 0) {
System.out.println("Security information deleted successfully.");
} else {
System.out.println("Security not found for deletion.");
}
} catch (SQLException e) {
handleSQLException(e);
}
}
private static void updatePaymentStatus(Statement statement) {
try {
System.out.println("Enter Citation Number to update payment status:");
int citationNum = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.println("Enter new Payment Status:");
String newPaymentStatus = scanner.nextLine();
String updateQuery = "UPDATE Citation SET paymentStatus = '" + newPaymentStatus + "' WHERE citationNum = " + citationNum;
int rowsAffected = statement.executeUpdate(updateQuery);
if (rowsAffected > 0) {
System.out.println("Payment status updated successfully.");
} else {
System.out.println("Citation not found for payment status update.");
}
} catch (SQLException e) {
handleSQLException(e);
}
}
private static void generateCitation(Statement statement) {
try{
System.out.println("Enter Citation Number:");
int citationNum = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.println("Enter Citation Time (HH:mm:ss):");
String citationTime = scanner.nextLine();
System.out.println("Enter Citation Date (YYYY-MM-DD):");
String citationDate = scanner.nextLine();
System.out.println("Enter Lot:");
int lot = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.println("Enter Payment Status:");
String paymentStatus = scanner.nextLine();
System.out.println("Enter Category (Invalid Permit,Expired Permit,No Permit) :");
String category = scanner.nextLine();
String insertQuery = "INSERT INTO Citation (citationNum, citationTime, citationDate, lot, paymentStatus, category) " +
"VALUES (" + citationNum + ", '" + citationTime + "', '" + citationDate + "', " + lot + ", '" +
paymentStatus + "', '" + category + "')";
int rowsAffected = statement.executeUpdate(insertQuery);
if (rowsAffected > 0) {
System.out.println("Citation generated successfully.");
} else {
System.out.println("Failed to generate citation.");
}
} catch (SQLException e) {
handleSQLException(e);
}
}
private static void handleSQLException(SQLException e) {
System.err.println("SQL Exception: " + e.getMessage());
// Handle specific SQL exceptions
if (e.getSQLState().equals("23000")) {
// SQLState "23000" corresponds to a violation of a unique constraint (e.g., PRIMARY KEY or FOREIGN KEY)
System.err.println("Error: Duplicate key or foreign key violation.");
} else if (e.getSQLState().equals("23001")) {
// SQLState "23001" corresponds to a violation of a CHECK constraint
System.err.println("Error: Check constraint violation.");
} else {
// Display the SQL exception message for all other errors
System.err.println("SQL State: " + e.getSQLState());
System.err.println("Error Code: " + e.getErrorCode());
}
}
}