-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathHomeController.java
More file actions
182 lines (152 loc) · 5.5 KB
/
Copy pathHomeController.java
File metadata and controls
182 lines (152 loc) · 5.5 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controllers;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.util.Callback;
import utils.ConnectionUtil;
/**
* FXML Controller class
*
* @author oXCToo
*/
public class HomeController implements Initializable {
@FXML
private TextField txtFirstname;
@FXML
private TextField txtLastname;
@FXML
private TextField txtEmail;
@FXML
private DatePicker txtDOB;
@FXML
private Button btnSave;
@FXML
private ComboBox<String> txtGender;
@FXML
Label lblStatus;
@FXML
TableView tblData;
/**
* Initializes the controller class.
*/
PreparedStatement preparedStatement;
Connection connection;
public HomeController() {
connection = (Connection) ConnectionUtil.conDB();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
txtGender.getItems().addAll("Male", "Female", "Other");
txtGender.getSelectionModel().select("Male");
fetColumnList();
fetRowList();
}
@FXML
private void HandleEvents(MouseEvent event) {
//check if not empty
if (txtEmail.getText().isEmpty() || txtFirstname.getText().isEmpty() || txtLastname.getText().isEmpty() || txtDOB.getValue().equals(null)) {
lblStatus.setTextFill(Color.TOMATO);
lblStatus.setText("Enter all details");
} else {
saveData();
}
}
private void clearFields() {
txtFirstname.clear();
txtLastname.clear();
txtEmail.clear();
}
private String saveData() {
try {
String st = "INSERT INTO wip_users ( firstname, lastname, email, gender, dob) VALUES (?,?,?,?,?)";
preparedStatement = (PreparedStatement) connection.prepareStatement(st);
preparedStatement.setString(1, txtFirstname.getText());
preparedStatement.setString(2, txtLastname.getText());
preparedStatement.setString(3, txtEmail.getText());
preparedStatement.setString(4, txtGender.getValue().toString());
preparedStatement.setString(5, txtDOB.getValue().toString());
preparedStatement.executeUpdate();
lblStatus.setTextFill(Color.GREEN);
lblStatus.setText("Added Successfully");
fetRowList();
//clear fields
clearFields();
return "Success";
} catch (SQLException ex) {
System.out.println(ex.getMessage());
lblStatus.setTextFill(Color.TOMATO);
lblStatus.setText(ex.getMessage());
return "Exception";
}
}
private ObservableList<ObservableList> data;
String SQL = "SELECT * from wip_users";
//only fetch columns
private void fetColumnList() {
try {
ResultSet rs = connection.createStatement().executeQuery(SQL);
//SQL FOR SELECTING ALL OF CUSTOMER
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1).toUpperCase());
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tblData.getColumns().removeAll(col);
tblData.getColumns().addAll(col);
System.out.println("Column [" + i + "] ");
}
} catch (Exception e) {
System.out.println("Error " + e.getMessage());
}
}
//fetches rows and data from the list
private void fetRowList() {
data = FXCollections.observableArrayList();
ResultSet rs;
try {
rs = connection.createStatement().executeQuery(SQL);
while (rs.next()) {
//Iterate Row
ObservableList 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);
}
tblData.setItems(data);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}