100 Days Coding Challenge - Day 6/100 [Displaying Database Values in JavaFX TableView]

in #bigwaves6 years ago

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

StepDirection
Step 1Create a new class for the User
Step 2In the class, create the method a public user method to accept string values carrying information about the user.
Step 3Generate 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 5Create an Initialize method as seen below.

29s1nreex6.png

 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.
8kms34rnnt.png


That's most of all way done today, anyone interested in solving a problem of this nature can drop a comment.


Thanks for following, commenting, upvoting... See you next time.

Official-Hord

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63475.77
ETH 3117.23
USDT 1.00
SBD 3.94