100 Days Coding Challenge - Day 6/100 [Displaying Database Values in JavaFX TableView]
Hello steemians, How was your day today? I practically had a great day thanks to Strapon Nation and a few others I can't mention.
What did I do today ?
Yesterday I had issues with displaying data from the database, though it worked with dummy data but I had issues pulling from the database as it kept diplaying errors. Anyways I took time to go through my code again applying a technique learnt from a friend and I came up with solutions to the problem.
Problem - Displaying data from database in JavaFx TableView
Solution
Step | Direction |
---|---|
Step 1 | Create a new class for the User |
Step 2 | In the class, create the method a public user method to accept string values carrying information about the user. |
Step 3 | Generate the set and get methods for all the details, If you're making use of IntelliJ platform, Right Click in your code and slect Generate, selct settter and getter in the new list. |
Step 4 | - Back to the Controller Class - Create an observable list, TableView, and TableColumns for the table. |
Step 5 | Create an Initialize method as seen below. |
public void initialize() throws Exception {
Connection conn = DbConnection.DbConnector();
ResultSet rs = conn.createStatement().executeQuery("Select * from customerDetails");
while (rs.next()) {
userlist.add(new User(rs.getString("CustomerID"), rs.getString("Surname"),
rs.getString("Firstname"), rs.getString("Telephone"), rs.getString("Address"),
rs.getString("email"), rs.getString("Title")));
}
custIdcolumn.setCellValueFactory(new PropertyValueFactory<>("CustomerId"));
surnamecolumn.setCellValueFactory(new PropertyValueFactory<>("Surname"));
firstnamecolumn.setCellValueFactory(new PropertyValueFactory<>("firstname"));
telcolumn.setCellValueFactory(new PropertyValueFactory<>("Telephone"));
addresscolumn.setCellValueFactory(new PropertyValueFactory<>("Address"));
emailcolumn.setCellValueFactory(new PropertyValueFactory<>("email"));
Titlecolumn.setCellValueFactory(new PropertyValueFactory<>("Title"));
table.setItems(userlist);
}
If done correctly, you'll see the data in the database displayed in the table as seen in the screenshot below.
That's most of all way done today, anyone interested in solving a problem of this nature can drop a comment.