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

时间:2019-04-25 13:50:37

标签: java javafx fxml

开始学习Java语言。我真的很喜欢Scene Builder技术。我试图在GUI中显示MariaDB数据库中的表,但出现无法解决的错误。如果您理解,请帮助。我完全删除了代码。

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;
        }
    }
}

模型/Person.java

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;
    }
}

view / 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中的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);

    }

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

  

“ C:\ Program Files \ Java \ jdk1.8.0_211 \ bin \ java.exe”   “ -javaagent:C:\ Program Files \ JetBrains \ IntelliJ IDEA社区版   2019.1 \ lib \ idea_rt.jar = 50017:C:\ Program Files \ JetBrains \ IntelliJ IDEA社区版2019.1 \ bin“ -Dfile.encoding = UTF-8 -classpath   “ C:\ Program Files \ Java \ jdk1.8.0_211 \ jre \ lib \ charsets.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ deploy.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ access-bridge-64.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ cldrdata.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ dnsns.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ jaccess.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ jfxrt.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ localedata.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ nashorn.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ sunec.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ sunjce_provider.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ sunmscapi.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ sunpkcs11.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ ext \ zipfs.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ javaws.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ jce.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ jfr.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ jfxswt.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ jsse.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ management-agent.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ plugin.jar; C:\ Program   Files \ Java \ jdk1.8.0_211 \ jre \ lib \ resources.jar; C:\ Program   文件\ Java \ jdk1.8.0_211 \ jre \ lib \ rt.jar; C:\ Projects \ JavaFXTest \ out \ production \ JavaFXTest; C:\ Users \ ccpraktikant \ Downloads \ mariadb-java-client-2.4.1.jar ”   sample.Main连接成功!应用程序启动方法异常   java.lang.reflect.InvocationTargetException在   sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)     在   com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:767)   由以下原因引起:java.lang.RuntimeException:应用程序启动中的异常   方法   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)     在   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 159(LauncherImpl.java:182)     在java.lang.Thread.run(Thread.java:748)造成原因:   javafx.fxml.LoadException:   /C:/Projects/JavaFXTest/out/production/JavaFXTest/sample/view/FXMLSample.fxml

     在

  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)在   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)在   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)在   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)在   javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)在   sample.Main.start(Main.java:13)位于   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 166(LauncherImpl.java:863)     在   com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 179(PlatformImpl.java:326)     在   com.sun.javafx.application.PlatformImpl.lambda $ null $ 177(PlatformImpl.java:295)     在java.security.AccessController.doPrivileged(本机方法)在   com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 178(PlatformImpl.java:294)     在   com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)     在com.sun.glass.ui.win.WinApplication._runLoop(本机方法)处   com.sun.glass.ui.win.WinApplication.lambda $ null $ 152(WinApplication.java:177)     ... 1更多原因:java.lang.NullPointerException   sample.controller.Controller.initData(Controller.java:77)位于   sample.controller.Controller.initialize(Controller.java:65)在   javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)...还有17个   正在运行应用程序示例异常。Main

     

以退出代码1完成的过程

2 个答案:

答案 0 :(得分:2)

如果您查看

initData();

和您的方法

private void initData(ActionEvent event) throws SQLException {

该方法带有参数,您的调用没有任何参数。

我建议您改写不带参数的方法。

private void initData() throws SQLException {

答案 1 :(得分:0)

我按如下方式重写了控制器,并且一切正常。

controller / Controller.java

terraform state mv -state=db.tfstate -state-out=../cluster/cluster.tfstate module.root module.db