-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewTest.java
More file actions
143 lines (122 loc) · 4.83 KB
/
TableViewTest.java
File metadata and controls
143 lines (122 loc) · 4.83 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
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
package kitordersystem;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import javafx.util.Callback;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* The class that shows everything in the database all at once. Excuse the name,
* I put it in as a test to see if it would work and then it worked so well that
* I just never changed it. This takes a global variable from the main menu for
* the ordering.
*/
public class TableViewTest extends Application {
private TableView tableview = new TableView();
// MAIN EXECUTOR
public static void main(String[] args) {
launch(args);
}
// CONNECTION DATABASE
/**
* @throws SQLException
* @throws IOException
*/
public void buildData() throws SQLException, IOException {
Connection c;
ObservableList<ObservableList> data = FXCollections.observableArrayList();
try {
c = new getConnection().getConnection();
String SQL = "USE mydb";
c.createStatement().executeQuery(SQL);
// SQL FOR SELECTING TABLES
String ordering = MainMenu.ordering;
String order;
switch (ordering){
case "Name A-Z": order = "Name ASC";
break;
case "Name Z-A": order = "Name DESC";
break;
case "Item": order = "Orders";
break;
case "Order ID": order = "ID";
break;
case "Squad": order = "Squad";
break;
case "Customer ID:":order = "CustomerID";
break;
case "Payment Method": order = "PaymentMethod";
break;
default: order = "ID";
}
SQL = "select o.ID, o.CustomerID, c.Name, c.Email_Address, c.Squad," +
" o.Orders, o.OrderSize, o.OrderNumber, o.NameOnGarment," +
" o.PaidFor, o.PaymentMethod, i.Item from Orders o INNER" +
" JOIN Customers c ON o.CustomerID = c.ID INNER JOIN " +
"Items i ON i.idItems=o.Orders ORDER BY " + order +";";
// ResultSet
ResultSet rs = c.createStatement().executeQuery(SQL);
/**
* Gets column headings and adds them to the TableView dynamically
*/
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
// Adds the data from each column into a list
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData()
.getColumnName (i + 1));
col.setCellValueFactory(
new Callback<TableColumn
.CellDataFeatures<ObservableList , String>,
ObservableValue<String>>() {
public ObservableValue<String> call(TableColumn
.CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.
getValue().get(j).toString());
}
});
tableview.getColumns().addAll(col);
System.out.println("Column [" + i + "] ");
}
/********************************
*
* Data added to ObservableList *
*
********************************/
while (rs.next()) {
// Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
// Iterate Column
row.add(rs.getString(i));
}
System.out.println("Row [1] added " + row);
data.add(row);
}
// FINALLY ADDED TO TableView
tableview.setItems(data);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
/**
* Makes the whole
*/
@Override
public void start(Stage stage) throws Exception {
// TableView
buildData();
// Main Scene
Scene scene = new Scene(tableview);
stage.setScene(scene);
stage.show();
}
}