-
Notifications
You must be signed in to change notification settings - Fork 1
/
FlightsDatabase.java
335 lines (248 loc) · 8.68 KB
/
FlightsDatabase.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FlightsDatabase {
/**************************************/
/********** GLOBAL VARIABLES **********/
/**************************************/
private Connection con;
private final String table_name = "flights", trig_name = "flights_trig";
private final static String SELECT = "select ",
CREATE_TABLE = "create table ",
CREATE_VIEW = "create or replace view ",
CREATE_TRIG = "create or replace trigger ",
DROP_TABLE = "drop table ",
INSERT = "insert into ";
/*********************************/
/********** Constructor **********/
/*********************************/
/**
* A constructor for the Class FlightsDatabase
* @input login the login name of the user, in the database
* @password the password of the user, in the database
*/
public FlightsDatabase(String login, String password) {
con = createConnection(login, password);
if(con == null)
System.exit(-1);
}
public static Connection createConnection(String login, String password) {
System.out.println("-------- Oracle JDBC Establishing Connection ------");
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return con;
}
System.out.println("Oracle JDBC Driver Registered!");
try {
con = DriverManager
.getConnection("jdbc:oracle:thin:@localhost:1521/xe",
login, password);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return con;
}
if (con != null) {
System.out.println("Connection Successful");
return con;
}
System.out.println("ERROR: Failed to make connection!");
return con ;
}
/***********************************/
/********** Create Method **********/
/***********************************/
/**
* The procedure creates a table in the Oracle database.
* The form of the table is:
* CREATE TABLE Flights(
* fno number(4) NOT NULL,
* ffrom varchar2(30) NOT NULL,
* fto varchar2(30) NOT NULL
* cost number(4) NOT NULL);
* @return true if the table was created and else return false
*/
public boolean create() {
System.out.println("Creating " + table_name + " Table...");
String query = CREATE_TABLE + table_name + " ("
+ " fno number(4) NOT NULL,"
+ " ffrom varchar2(30) NOT NULL,"
+ " fto varchar2(30) NOT NULL,"
+ " cost number(4) NOT NULL"
+ ")";
try {
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
} catch (SQLException e) {
if(e.getErrorCode() == 955) {
System.out.println("ERROR: Table Already Exists");
return false;
}
e.printStackTrace(); //DEBUG
System.out.println("ERROR: Failed to Create Table");
return false;
}
System.out.println(table_name + " Table Created");
return true;
}
/***************************************/
/********** addTrigger Method **********/
/***************************************/
/**
* The procedure creates a trigger on flights
* This trigger check for every insert –
* That the pno number is less then 1000, if not it set the number to max(pno)+1
* @return true if the table was created and else return false
*/
public boolean addTrigger() {
String query = CREATE_TRIG + trig_name
+ " BEFORE INSERT ON " + table_name
+ " FOR EACH ROW"
+ " DECLARE maxfno NUMBER;"
+ " BEGIN"
+ " IF :NEW.fno > 999 THEN"
+ " SELECT MAX(fno)+1 INTO maxfno FROM Flights;"
+ " IF maxfno IS NOT NULL THEN"
+ " :NEW.fno := maxfno;"
+ " ELSE"
+ " :NEW.fno := 1;"
+ " END IF;"
+ " END IF;"
+ " END;";
try {
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace(); //DEBUG
System.out.println("ERROR: Failed to Create Trigger");
return false;
}
System.out.println(trig_name + " Trigger On " + table_name + " Created");
return true;
}
/***********************************/
/********** Insert Method **********/
/***********************************/
/**
* The procedure inserts a flight to the table Flights in the database.
* @input route the flight’s number
* @input origin the name of the origin of the flight
* @input destination the name of the destination of the interval
* @input price the flight’s cost
* @return true if the insertion succeeded and else false
*/
public boolean insert(int route, String origin, String destination, int price) {
String query = INSERT + table_name
+ " values (" + route
+ ",'" + origin + "'"
+ ",'" + destination +"'"
+ "," + price + ")";
try {
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace(); //DEBUG
System.out.println("ERROR: Failed to insert row");
return false;
}
System.out.println("Row Inserted Successfuly");
return true;
}
/**********************************/
/********** Clean Method **********/
/**********************************/
/**
* The procedure drops the table Flights from the Oracle database.
* @return true if the table has been removed successfully and else false
*/
public boolean clean() {
System.out.println("Dropping " + table_name + " Table...");
String query = DROP_TABLE + " " + table_name;
try {
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("ERROR: Failed to Drop Table");
return false;
}
System.out.println(table_name + " Table Dropped");
return true;
}
/*******************************************/
/********** CheapestFlight Method **********/
/*******************************************/
/**
* The procedure tells if two given places are indirectly connected.
* If there are no routes, in the database, that allow to get from
* place1 to place2 the procedure returns -1, else it returns the cost of the cheapest path between place1 and place2. .
* @input place1 the name of the source of the path
* @input place2 the name of the destination of the path
* @return the cheapest cost of flights (if the places are connected by a path) and else return -1
*/
public int CheapestFlight(String place1, String place2) {
if(place1.equals(place2))
return 0;
int cheapest = Integer.MAX_VALUE;
String views[] = {"trip1", "trip2", "trip3"};
String query1 = CREATE_VIEW + views[0] + " AS " +
SELECT + "fto AS ffrom, cost AS total_cost "
+ "FROM flights "
+ "WHERE ffrom = '" + place1 + "' "
+ "ORDER BY total_cost ASC";
String query2 = CREATE_VIEW + views[1] + " AS " +
SELECT + "f.fto AS ffrom, f.cost + tp.total_cost AS total_cost "
+ "FROM flights f JOIN trip1 tp ON f.ffrom = tp.ffrom "
+ "ORDER BY total_cost ASC";
String query3 = CREATE_VIEW + views[2] + " AS " +
SELECT + "f.fto AS ffrom, f.cost + tp.total_cost AS total_cost "
+ "FROM flights f JOIN trip2 tp ON f.ffrom = tp.ffrom "
+ "ORDER BY total_cost ASC";
String queries[] = {query1, query2, query3};
for(int i = 0; i < 3; i++) {
try {
Statement stmt = con.createStatement();
stmt.executeQuery(queries[i]);
ResultSet rs = stmt.executeQuery("SELECT * FROM trip" + (i+1));
while(rs.next()) {
String connection = rs.getString("ffrom");
if(connection.equals(place2)) {
int cost = rs.getInt("total_cost");
if(cheapest > cost)
cheapest = cost;
}
}
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
if(cheapest == Integer.MAX_VALUE)
cheapest = -1;
return cheapest;
}
/***********************************/
/********** DEBUG METHODS **********/
/***********************************/
public void printTable() {
String query = "SELECT * FROM flights ORDER BY fno ASC";
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("--------------------------------------");
System.out.println("FNO \t FFROM \t FTO \t COST\t");
while(rs.next())
System.out.println(rs.getInt("fno") + " \t " + rs.getString("ffrom")
+ " \t " + rs.getString("fto") + " \t " + rs.getInt("cost"));
System.out.println("--------------------------------------");
} catch (SQLException e) {
e.printStackTrace();
}
}
}