-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBHandler.java
272 lines (209 loc) · 9.29 KB
/
DBHandler.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package DatabaseHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import BusinessLayer.Booking;
import BusinessLayer.Registration;
import BusinessLayer.Station;
import BusinessLayer.Train;
import BusinessLayer.admin;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
public class DBHandler{
private static DBHandler instance = null;
private DBHandler()
{}
public static synchronized DBHandler getInstance()
{
if(instance == null)
{
instance = new DBHandler();
}
return instance;
}
public Boolean BookingConfirmed(String cnic, String nameofTrain, String arrival, String destination, int no_of_Seats) throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
PreparedStatement ps = con.prepareStatement("select * from customer where customer.cnic='"+cnic+"';");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
cnic = rs.getString("cnic");
if(!cnic.contentEquals(cnic))
{
String Errors = "Cannot book the ticket due to invalid or registered cnic";
Alert errorAlert = new Alert(AlertType.ERROR);
errorAlert.setTitle("Booking Unsuccessful");
errorAlert.setContentText(Errors);
errorAlert.show();
return false;
}
}
PreparedStatement ps1 = con.prepareStatement("select * from train where train.NameOfTrain='"+nameofTrain+"'AND train.arrival='"+arrival+"' AND train.destination='"
+destination+"';");
ResultSet rs1 = ps1.executeQuery();
//
// while(rs1.next())
// {
// String train_Name = rs1.getString("NameOfTr");
// String train_arrival = rs1.getString("arrival");
// String train_destination = rs1.getNString("destination");
//
// if(!(train_Name.contentEquals(nameofTrain)) && (train_arrival.contentEquals(arrival)) && (train_destination.contentEquals(destination)))
// {
// String Errors = "Train does not exist! Please enter correct Train Name";
// Alert errorAlert = new Alert(AlertType.ERROR);
// errorAlert.setTitle("Booking Unsuccessful");
// errorAlert.setContentText(Errors);
// errorAlert.show();
// return false;
// }
// if(!train_arrival.contentEquals(arrival))
// {
// String Errors = "This specific train does not go from the entered Place!";
// Alert errorAlert = new Alert(AlertType.ERROR);
// errorAlert.setTitle("Booking Unsuccessful");
// errorAlert.setContentText(Errors);
// errorAlert.show();
// return false;
// }
//
// if(!train_destination.contentEquals(destination))
// {
// String Errors = "This specific train does not go to the entered Place!";
// Alert errorAlert = new Alert(AlertType.ERROR);
// errorAlert.setTitle("Booking Unsuccessful");
// errorAlert.setContentText(Errors);
// errorAlert.show();
// return false;
// }
// }
PreparedStatement pstmt = con.prepareStatement("INSERT INTO booking VALUES (?,?,?,?,?);");
pstmt.setString(1, cnic);
pstmt.setString(2, nameofTrain);
pstmt.setString(3, arrival);
pstmt.setString(4, destination);
pstmt.setInt(5, no_of_Seats);
pstmt.executeUpdate();
int fare = no_of_Seats*1000;
String fare_amount = "Calculated fare is "+ fare;
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Fare Amount");
alert.setContentText(fare_amount);
alert.show();
System.out.print("Calculated cost is ");
System.out.println(fare);
Booking bookTicket = new Booking(cnic,nameofTrain,arrival,destination,no_of_Seats);
return true;
}
public Boolean ValidateCustomerForCancelBooking(String cnic) throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
PreparedStatement ps = con.prepareStatement("select * from booking where booking.cnic='"+cnic+"';");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
cnic = rs.getString("cnic");
if(!cnic.contentEquals(cnic))
{
String Errors = "No ticket has been booked on the entered cnic";
Alert errorAlert = new Alert(AlertType.ERROR);
errorAlert.setTitle("Booking Unsuccessful");
errorAlert.setContentText(Errors);
errorAlert.show();
return false;
}
}
// PreparedStatement ps1 = con.prepareStatement("DELETE FROM booking where booking.cnic='"+cnic+"';");
Statement st = con.createStatement();
int rs1 = st.executeUpdate("DELETE FROM booking where booking.cnic='"+cnic+"';");
return true;
}
public Boolean addTrainConfirmed(String nameofTrain, String arrival, String destination) throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
PreparedStatement ps = con.prepareStatement("select * from train where train.NameOfTrain ='"+nameofTrain+"';");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
String train_name = rs.getString("NameOfTrain");
if(nameofTrain.contentEquals(train_name))
{
String Errors = "Cannot add the train due to invalid or already used train name";
Alert errorAlert = new Alert(AlertType.ERROR);
errorAlert.setTitle("add Train Unsuccessful");
errorAlert.setContentText(Errors);
errorAlert.show();
return false;
}
}
PreparedStatement ps1 = con.prepareStatement("Insert into train Values(?,?,?)");
ps1.setString(1, nameofTrain);
ps1.setString(2, arrival);
ps1.setString(3, destination);
ps1.executeUpdate();
Train addTrain = new Train(nameofTrain,arrival,destination);
return true;
}
public ResultSet BookingCnicExists(String updateBooking_Cnic) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
Statement ps = con.createStatement();
ResultSet rs = ps.executeQuery("select * from booking where booking.cnic ='"+updateBooking_Cnic+"';");
return rs;
}
public void updateBooking(String updateBooking_Cnic,String updateBooking_TrainName, String updateBooking_arrival,
String updateBooking_destination, int updateBooking_noOfSeats) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
String query = "update booking set trainName='"+updateBooking_TrainName+"',arrival='"+updateBooking_arrival+"',dest='"
+updateBooking_destination+"',numbOfSeats='"+updateBooking_noOfSeats+"' where cnic='"+updateBooking_Cnic+"';";
Statement st = con.createStatement();
int rs = st.executeUpdate(query);
}
public boolean validateTrainDeletion(String deleteTrain_Name) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
PreparedStatement ps = con.prepareStatement("select * from train where train.NameOfTrain='"+deleteTrain_Name+"';");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
String train_Name_del = rs.getString("NameOfTrain");
if(!train_Name_del.contentEquals(deleteTrain_Name))
{
String Errors = "Train does not Exist";
Alert errorAlert = new Alert(AlertType.ERROR);
errorAlert.setTitle("Train Not Found");
errorAlert.setContentText(Errors);
errorAlert.show();
return false;
}
}
// PreparedStatement ps1 = con.prepareStatement("DELETE FROM booking where booking.cnic='"+cnic+"';");
Statement st = con.createStatement();
int rs1 = st.executeUpdate("DELETE FROM train where train.NameOfTrain='"+deleteTrain_Name+"';");
return true;
}
public void train_UpdateValidate(String updateTrain_TrainName, String updateTrain_arrival, String updateTrain_dest) throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
String query = "update train set NameOfTrain='"+updateTrain_TrainName+"',arrival='"+updateTrain_arrival+"', destination='"
+updateTrain_dest+"';";
Statement st = con.createStatement();
int rs = st.executeUpdate(query);
}
public void addTrainStation(String stationName) throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/railwayschema", "root", "PASSWORD-123");
Station st = new Station(stationName);
}
}