当前位置: 首页 > 知识库问答 >
问题:

如何通过JavaFX将数据从Mariadb数据库获取到表中?

岳阳飙
2023-03-14

main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application  {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("view/FXMLSample.fxml"));
        Scene scene = new Scene(root, 640,480);

        primaryStage.setScene(scene);
        primaryStage.setTitle("User list");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Controller/Controller.java

package sample.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import sample.model.Person;
import sample.model.ConnectDB;

import javafx.scene.control.TableColumn;

import java.sql.*;

public class Controller implements Initializable {

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    private ObservableList<Person> usersData = FXCollections.observableArrayList();

    @FXML
    private TableView<Person> tableUsers;

    @FXML
    private TableColumn<Person, Integer> idColumn;

    @FXML
    private TableColumn<Person, String> usernameColumn;

    @FXML
    private TableColumn<Person, String> firstnameColumn;

    @FXML
    private TableColumn<Person, String> lastnameColumn;

    @FXML
    private TableColumn<Person, String> emailColumn;

    @FXML
    private TableColumn<Person, String> genderColumn;

    @FXML
    private TableColumn<Person, String> descColumn;

    @FXML
    private TableColumn<Person, String> createdonColumn;

    @FXML
    private TableColumn<Person, String> editedonColumn;

    @FXML
    private TableColumn<Person, String> activeColumn;

    @FXML
    public void initialize(URL location, ResourceBundle resources) {

        conn = ConnectDB.ConnectMariaDB();
        initData();

        tableUsers.setItems(usersData);

    }

    private void initData(ActionEvent event) throws SQLException {

        String sql = "SELECT * FROM test";
        rs = pst.executeQuery(sql);

        if(rs.next()) {
            int id = rs.getInt("id");
            String username = rs.getString("username");
            String firstname = rs.getString("firstname");
            String lastname = rs.getString("lastname");
            String email = rs.getString("email");
            String gender = rs.getString("gender");
            String description = rs.getString("description");
            String createdon = rs.getString("createdon");
            String editedon = rs.getString("editedon");
            String active = rs.getString("active");
            usersData.add(new Person(id, username, firstname, lastname, email, gender, description, createdon, editedon, active));
        }


    }


}

模型/ConnectDB.java

package sample.model;

import java.sql.*;

public class ConnectDB {

    Connection conn = null;

    public static Connection ConnectMariaDB() {
        try {
            Class.forName("org.mariadb.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost/test_new","root","123456");
            System.out.println("Connection success!");
            return conn;
        } catch(Exception e){
            System.out.println(e);
            return null;
        }
    }
}
package sample.model;

import javafx.beans.property.SimpleStringProperty;

public class Person {

    private int id;
    private String username;
    private String firstname;
    private String lastname;
    private String email;
    private String gender;
    private String description;
    private String createdon;
    private String editedon;
    private String active;

    public Person(int id, String username, String firstname, String lastname, String email, String gender, String description, String createdon, String editedon, String active) {
        this.id = id;
        this.username = username;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.gender = gender;
        this.description = description;
        this.createdon = createdon;
        this.editedon = editedon;
        this.active = active;
    }

    public Person() {
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCreatedon() {
        return createdon;
    }

    public void setCreatedon(String createdon) {
        this.createdon = createdon;
    }

    public String getEditedon() {
        return editedon;
    }

    public void setEditedon(String editedon) {
        this.editedon = editedon;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }
}

查看/fxmlSample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.Controller">
  <children>
    <MenuBar VBox.vgrow="NEVER">
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="New" />
            <MenuItem mnemonicParsing="false" text="Open…" />
            <Menu mnemonicParsing="false" text="Open Recent" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Close" />
            <MenuItem mnemonicParsing="false" text="Save" />
            <MenuItem mnemonicParsing="false" text="Save As…" />
            <MenuItem mnemonicParsing="false" text="Revert" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Preferences…" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Quit" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Edit">
          <items>
            <MenuItem mnemonicParsing="false" text="Undo" />
            <MenuItem mnemonicParsing="false" text="Redo" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Cut" />
            <MenuItem mnemonicParsing="false" text="Copy" />
            <MenuItem mnemonicParsing="false" text="Paste" />
            <MenuItem mnemonicParsing="false" text="Delete" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Select All" />
            <MenuItem mnemonicParsing="false" text="Unselect All" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Help">
          <items>
            <MenuItem mnemonicParsing="false" text="About MyHelloApp" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
    <AnchorPane id="AnchorPane" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
         <children>
            <Label layoutX="20.0" layoutY="14.0" text="Table" />
            <TableView fx:id="tableUsers" layoutX="20.0" layoutY="37.0" prefHeight="285.0" prefWidth="600.0">
              <columns>
                  <TableColumn prefWidth="42.0" text="ID" fx:id="idColumn" />
                  <TableColumn prefWidth="67.0" text="Username" fx:id="usernameColumn" />
                  <TableColumn prefWidth="156.0" text="Fullname">
                     <columns>
                        <TableColumn prefWidth="75.0" text="First name" fx:id="firstnameColumn" />
                        <TableColumn prefWidth="75.0" text="Last name" fx:id="lastnameColumn" />
                     </columns>
                  </TableColumn>
                <TableColumn prefWidth="75.0" text="E-mail" fx:id="emailColumn" />
                <TableColumn prefWidth="69.0" text="Gender" fx:id="genderColumn" />
                  <TableColumn prefWidth="69.0" text="Description" fx:id="descColumn" />
                  <TableColumn prefWidth="63.0" text="Created" fx:id="createdonColumn" />
                  <TableColumn minWidth="0.0" prefWidth="57.0" text="Edited" fx:id="editedonColumn" />
                  <TableColumn minWidth="4.0" prefWidth="105.0" text="Active" fx:id="activeColumn" />
              </columns>
            </TableView>
            <Button layoutX="23.0" layoutY="336.0" mnemonicParsing="false" text="Create" />
            <Button layoutX="88.0" layoutY="336.0" mnemonicParsing="false" text="Update" />
            <Button layoutX="154.0" layoutY="336.0" mnemonicParsing="false" text="Delete" />
            <Button layoutX="219.0" layoutY="336.0" mnemonicParsing="false" text="Refresh" />
         </children>
    </AnchorPane>
  </children>
</VBox>

错误:

错误:(64,9)Java:类Sample.Controller.Controller中的method initData无法应用于给定类型;必需:javafx.event.actionevent找到:没有参数原因:实际和正式参数列表的长度不同

private void initData() throws SQLException {
...
}
@FXML
    public void initialize(URL location, ResourceBundle resources) {

        conn = ConnectDB.ConnectMariaDB();
        try {
            initData();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        tableUsers.setItems(usersData);

    }

最后,我给出了一个错误。

在javafx.fxml.fxmlloader.constructloadexception(fxmlloader.java:2601)在javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2579)在javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2441)在javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3214)在javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3175)在在com.sun.glass.ui.win.winapplication._runloop(本机方法)在com.sun.glass.ui.win.winapplication.lambda$null$152(winapplication.java:177)...1另一个原因:在sample.controller.controller.initData(controller.initialize(controller.java:77)在sample.controller.initalize(controller.java:65)在javafx.fxml.fxmlloader.loadimpl(

进程已完成,退出代码为%1

共有1个答案

景宏富
2023-03-14

如果你看

InitData();

而你的方法,

private void initData(ActionEvent event) throws SQLException {
private void initData() throws SQLException {
 类似资料:
  • 我得到了这段代码,但我认为这是不好的方法,如果有像100K的结果,它会返回100K的新用户怎么办?对于,我是否可以使用其他方法,但我不太确定如何实现。另外,是否应该使用块?

  • 我想创建一个包含类别的列表,并将类别悬停在需要显示子类别的类别上。我能够在列表中显示父类别。但我不知道如何获得子类别。在我的表中有category\u id、parent\u id列和一些其他列。如果父类别id为“0”,则它是主类别,对于子类别,它包含类别id。因此,现在我需要显示主类别的子类别。我不明白该如何进行。谁能给我提建议。

  • 我创建了一个名为的类来从firebase数据库中获取所有用户数据。下面是我用来获取firebase数据(用户名)的方法。 而firebase链接也没有问题。它还提供了用户名。 当我用一些虚拟的预定义字符串数据替换username时,这段代码就可以工作了。但是当我添加用户名时,它会给我以下错误。

  • 问题内容: 有没有一种方法可以通过Hibernate 3.2 API获得一些基础数据库版本的信息?我在这里和javadoc中都找不到相关的位。 问题答案: 获取数据库引擎的版本是特定于实现的。这意味着没有获取版本的共享方法,因此Hibernate不能真正提供API,因为它没有绑定到任何特定的RDBMS。例如,以下是通过一些著名的RDBMS使用SELECT语句获取版本的几种不同方法: Oracle

  • 问题内容: 我已经搜索了许多网站,但没有遇到任何从单个数据库获取表名的细节的代码或教程。 假设我有4个数据库,并且我想要数据库中所有表的名称,我可以使用什么查询? 问题答案:

  • 问题内容: 我正在基于CodeIgniter的应用程序。这里的代码: 控制器 : 型号 : 查看 : Javascript : 上面的代码运行良好。 现在,我想通过ajax请求将数据提取到。我已经从url创建了一个ajax请求,可以从这里,where 和进行说。 Ajax响应产生: 问题 如何使用Ajax数据源配置数据表,以及如何将数据显示到表中,所以我可以使用数据例如创建类似代码的链接 问题答案