-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTrain_Management.java
40 lines (39 loc) · 1.61 KB
/
SearchTrain_Management.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
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class SearchTrain_Management {
public ArrayList <SearchTrain_Train> viewTrain (String coachType, String source, String destination){
// Fill your code here
ArrayList<SearchTrain_Train> trainList = new ArrayList<SearchTrain_Train>();
try {
Connection con = SearchTrain_DB.getConnection();
Statement statement = con.createStatement();
String query = "SELECT * FROM train WHERE source = '" + source + "' AND destination = '" + destination+ "' AND " + coachType + " != 0 ORDER BY train_number";
ResultSet result = statement.executeQuery(query);
while(result.next()){
int train_number = result.getInt(1);
String train_name = result.getString(2);
String sc = result.getString(3);
String ds = result.getString(4);
int ac1 = result.getInt(5);
int ac2 = result.getInt(6);
int ac3 = result.getInt(7);
int sleeper = result.getInt(8);
int seater = result.getInt(9);
trainList.add(new SearchTrain_Train(train_number,train_name,sc,ds,ac1,ac2,ac3,sleeper,seater));
}
statement.close();
con.close();
return trainList;
}
catch(SQLException e) {
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
return null;
}
}