JavaFX uses scene builder to initialize TableView

It has just been implemented. When you click to switch to this page, the data queried in the MySQL database will be displayed

MySQL database needs to create an initialization data table

Connect layer, create entity class

A Student object, used as a generic for subsequent traversal of the collection

package pojo;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

//Create a new class, and after determining the data, construct the data model
//This place is a big pit. The data type here cannot be defined directly. It must be defined as a data type similar to the one below.
public class Student {
    private final SimpleIntegerProperty id;
    private final SimpleStringProperty name;
    private final SimpleStringProperty gender;
    private final SimpleIntegerProperty age;
    private final SimpleStringProperty profession;
    private final SimpleIntegerProperty phone;


    //Construction with parameters
    public Student(int id, String name, String gender, int age, String profession, int phone){
        this.id = new SimpleIntegerProperty(id);
        this.name = new SimpleStringProperty(name);
        this.gender = new SimpleStringProperty(gender);
        this.age = new SimpleIntegerProperty(age);
        this.profession =new SimpleStringProperty(profession);
        this.phone = new SimpleIntegerProperty(phone);
    }


   //get, set methods
    public int getId() {
        return id.get();
    }

    public SimpleIntegerProperty idProperty() {
        return id;
    }

    public void setId(int id) {
        this.id.set(id);
    }

    public String getName() {
        return name.get();
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getGender() {
        return gender.get();
    }

    public SimpleStringProperty genderProperty() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender.set(gender);
    }

    public int getAge() {
        return age.get();
    }

    public SimpleIntegerProperty ageProperty() {
        return age;
    }

    public void setAge(int age) {
        this.age.set(age);
    }

    public String getProfession() {
        return profession.get();
    }

    public SimpleStringProperty professionProperty() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession.set(profession);
    }

    public int getPhone() {
        return phone.get();
    }

    public SimpleIntegerProperty phoneProperty() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone.set(phone);
    }
}
Use Scene Builder to create an fxml page
Load database data in the controller class

There is an initialization page at the beginning. When you enter this page, it is the controller class bound by fxml.

Bind the fxml button in this class

package Students;

import dao.JDBC;
import dao.students;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import pojo.Student;

import java.sql.ResultSet;

public class a_information {


    //Return to the previous interface
    @FXML
    private Button rtn;
    public void set_return(MouseEvent mouseEvent) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/Students/user.fxml"));
        rtn.getScene().setRoot(root);
    }


// Declare each control
    @FXML
    private TableView<Student> tableView;;
    @FXML
    private TableColumn<Student,Integer>id;
    @FXML
    private TableColumn<Student,String> name;
    @FXML
    private TableColumn <Student,String> gender;
    @FXML
    private TableColumn <Student,Integer>age;
    @FXML
    private TableColumn<Student,String> profession;
    @FXML
    private TableColumn<Student,Integer> phone;


    //This method is used to insert the initialized table (loadData(); below) into the queried data when the page is loaded.

    //Because when the FXML file is loaded and associated with the controller class, JavaFX will automatically create an instance of the controller class,
    // And automatically call the initialize() method. Inside the initialize() method, the loadData() method is called,
    // This enables automatic loading of data when loading an FXML file.
    @FXML
    public void initialize() throws Exception{
        loadData();
    }




    //This method is used to initialize the table
    public void loadData() throws Exception{
        String sql = "SELECT id, name, gender, age, profession, phone FROM small_systems.student";

        
        //This place encapsulates JDBC connections, etc., so sql statements can be executed directly
        ResultSet rs = JDBC.executeQuery(sql);
        ObservableList<Student> studentList = FXCollections.observableArrayList();

        
        //Loop, get every number in the database, and add it to the value defined above
        //In the studentList collection with Student as the generic type
        while (rs.next()) {
            int studentId = rs.getInt("id");
            String studentName = rs.getString("name");
            String studentGender = rs.getString("gender");
            int studentAge = rs.getInt("age");
            String studentProfession = rs.getString("profession");
            int studentPhone = rs.getInt("phone");

            Student student = new Student(studentId, studentName, studentGender, studentAge, studentProfession, studentPhone);
            studentList.add(student);
        }

        //Set each data so that each data is displayed
        id.setCellValueFactory(new PropertyValueFactory<>("id"));
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        gender.setCellValueFactory(new PropertyValueFactory<>("gender"));
        age.setCellValueFactory(new PropertyValueFactory<>("age"));
        profession.setCellValueFactory(new PropertyValueFactory<>("profession"));
        phone.setCellValueFactory(new PropertyValueFactory<>("phone"));

        tableView.setItems(studentList);

    }

}

Finally, add the JDBC encapsulation class

package dao;

import java.sql.*;
import java.util.Objects;

public class JDBC {
        /**
         * URL address
         */
        private static final String URL = "jdbc:mysql://127.0.0.1:3306/small_systems?serverTimezone=UTC";
        /**
         * Account to log in to the database server
         */
        private static final String USER = "root";
        /**
         * Password to log in to the database server
         */
        private static final String PASSWORD = "123456";

        /**
         * Return the database connection object
         *
         * @return
         */
        public static Connection getConn() {
            try {
                return DriverManager.getConnection(URL, USER, PASSWORD);
            } catch (SQLException e) {
                System.out.println("Connection failed");
                e.printStackTrace();
            }
            return null;
        }

        /**
         * Close the resource
         *
         * @param rs
         * @param stat
         * @param conn
         */
        public static void close(ResultSet rs, Statement stat, Connection conn) {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stat != null) {
                    stat.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        /**
         * Encapsulates general update operations (that is, using this method to implement insert, update, and delete operations for any data table)
         *
         * @param sql SQL statement to be executed
         * @param params Parameters need to be passed when executing SQL statements
         * @return execution result
         */
        public static boolean exeUpdate(String sql, Object... params) {
            //Get the connection object
            Connection conn = getConn();
            PreparedStatement ps = null;
            try {
                //Get the precompiled object
                ps = conn.prepareStatement(sql);
                //Perform parameter assignment operation
                if (Objects.nonNull(params)) {
                    //Loop to assign values to all parameters
                    for (int i = 0; i < params.length; i + + ) {
                        ps.setObject(i + 1, params[i]);
                    }
                }
                //Perform update
                return ps.executeUpdate() > 0;
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                //Close the resource
// close(null, ps, conn);
            }
            return false;
        }



        //Inquire
    public static ResultSet executeQuery(String sql, Object... params) {
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            //Get the precompiled object
            ps = conn.prepareStatement(sql);
            //Perform parameter assignment operation
            if (Objects.nonNull(params)) {
                //Loop to assign values to all parameters
                for (int i = 0; i < params.length; i + + ) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            //Perform update
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //Close the resource
// close(null, ps, conn);
        }
        return null;
    }


}

In addition, problems you may encounter include errors reported due to unexposed packages. You need to manually expose them in the module-info.java file.

Note: Because I am a beginner, I don’t know much about many local functions and some data types. Some remarks and explanations are just my personal understanding.

Please forgive me if there are any errors!

I’ve been stuck for several days and searched a lot. I just recorded it myself.