如何显示来自MySQL的数据?

时间:2020-02-25 17:38:57

标签: mysql javafx

我有一个数据库“ workschedule”和一个表“ employee”。但是当我尝试显示该表中的数据时,我只能看到该表中的最后一条记录

ObservableList<Variable> employeeObList  = FXCollections.observableArrayList();
    columnId.setCellValueFactory(new PropertyValueFactory<Variable, Integer>("id"));
    columnName.setCellValueFactory(new PropertyValueFactory<Variable, String>("name"));
    ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM workchedule.employees;");
    try {
        while (rs.next()) {
            employeeObList.add(new Variable(rs.getInt("id"), rs.getString("name")));
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    finally {
        rs.close();
        conn.close();
    }
    employeeTableView.setItems(employeeObList);


@FXML private TableView<Variable> employeeTableView;
@FXML private TableColumn<Variable, Integer> columnId;
@FXML private TableColumn<Variable, String> columnName;

变量-一个用getter和setter保存变量的类

Image from MySQL Image from a scene

1 个答案:

答案 0 :(得分:0)

所以使用jdbc时,我总是这样:

负责连接到数据库的连接器:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Connector {
    public Connection con = null;
    final String URL = "jdbc:mysql://localhost:3306/dbname";
    final String USERNAME = "username";
    // Not so safe but easy
    final String PASSWORD = "1234";

    /*
     * Adds DB Connection
     */
    public void add() {
        try {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
}

然后我使用DAO从数据库中获取数据:

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import application.controller.Connector;

public class Dao{

    Connector c = new Connector();

    public ArrayList<Model> selectAllData() {
        ArrayList<Model> list = new ArrayList<>();
        try {
            c.add();
            PreparedStatementstmt = c.con.prepareStatement("SELECT id FROM db.table");
            ResultSet result = stmt.executeQuery(
                    );
            while (result.next()) {
                Model model= new Model();
                 //here set all the attributes
                model.setId(result.getInt("id"));
                list.add(model);
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } finally {
            if (c.con != null) {
                try {
                    c.con.close();
                } catch (SQLException se) {
                    se.printStackTrace();
                }
            }
        }
        return list;
    }

然后在JavaFX Controller中,我可以调用DAO函数。

@FXML
    public void initialize() {
        initTable();
    }

    /*
     * Sets up the table and maps the values
     */
    @SuppressWarnings("unchecked")
    private void initTable() {
        idCol.setCellValueFactory(new PropertyValueFactory<Model, Integer>("id"));
        loadTableData();
    }

    /*
     * Loads all the data into table
     */
    private void loadTableData() {
        models = dao.selectAllData();
        data = FXCollections.observableArrayList(models);
        table.setItems(data);
    }