-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTripDisplayGUI.java
More file actions
80 lines (70 loc) · 2.78 KB
/
TripDisplayGUI.java
File metadata and controls
80 lines (70 loc) · 2.78 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.Vector;
public class TripDisplayGUI {
JFrame frame = new JFrame(
"Available Trips");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(600,300);
String[] columnNames = { "Trip ID", "City", "Start Date", "End Date", "Tour Name" };
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);frame.add(scrollPane);
fetchData(model);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
private static Connection openDatabase() {
String url = "jdbc:mysql://localhost:3306/MTG";
String user = "root";
String password = "Syetem";
try {
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static void closeDatabase(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static ResultSet executeQuery(Connection connection, String query) {
try {
Statement statement = connection.createStatement();
return statement.executeQuery(query);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static void fetchData(DefaultTableModel model) {
Connection connection = openDatabase();
if (connection != null) {
String query = "SELECT TRIP.TRIP_ID, CITIES.CITY_NAME, TRIP.START_DATE, TRIP.END_DATE, ADDITIONAL_SERVICES.TOUR_GUIDE_NAME "
+
"FROM TRIP " +
"JOIN CITIES ON TRIP.CITY_ID = CITIES.CITY_ID " +
"JOIN ADDITIONAL_SERVICES ON TRIP.CUSTOMER_ID = ADDITIONAL_SERVICES.CUSTOMER_ID";
ResultSet resultSet = executeQuery(connection, query);
try {
while (resultSet != null && resultSet.next()) {
Vector<Object> row = new Vector<>();
row.add(resultSet.getInt("TRIP_ID"));
row.add(resultSet.getString("CITY_NAME"));
row.add(resultSet.getDate("START_DATE"));
row.add(resultSet.getDate("END_DATE"));
row.add(resultSet.getString("TOUR_GUIDE_NAME"));
model.addRow(row);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeDatabase(connection);
}
}
}
}