java - 用数据库数据填充表不起作用

标签 java mysql sql javafx tableview

我一直在尝试加载带有从数据库查询的数据的 TableView,但似乎无法让它工作。

这是我第一次尝试用数据库查询项填充数据库,以防我的代码看起来困惑且远非良好。

FXML 是通过 JavaFx SceneBuilder 完成的。

这是数据库查询类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class StudentInfo {
    static String JDBC_DRIVER = "org.h2.Driver";
    static String DB_URL = "jdbc:h2:file:C:/WAKILI/WAKILIdb";
    //  Database credentials
    static final String USER = "sa";
    static final String PASS = "";

    public static Connection conn = null;

    public List<KIWIDataModel> getAllstudentInfo() {
        Statement st = null;
        ResultSet rs;
        String driver = "org.h2.Driver";

        List ll = new LinkedList();

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            st = conn.createStatement();
            String recordQuery = ("SELECT id, KIWI FROM KIWI");

            rs = st.executeQuery(recordQuery);
            while (rs.next()) {
                int Key = rs.getInt(1);

                ObservableList row = FXCollections.observableArrayList();

                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    row.add(rs.getString(i));
                    System.out.println(row);
                }

                KIWIDataModel roww = new KIWIDataModel();
                roww.setFirstName(Key);

                ll.add(row);
            }
        } catch (ClassNotFoundException | SQLException ex) {
            // CATCH SOMETHING
        }
        return ll;
    }
}

这是 Controller 类:

import DB.KIWI.Try.KIWIDataModel;
import DB.KIWI.Try.StudentInfo;
import DBQuerry.DynamicTable;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class SampleController implements Initializable {

    @FXML
    private TableView<KIWIDataModel> Table;
    @FXML
    private TableColumn<KIWIDataModel, Integer> colKey;

    @FXML
    public void selectKIWITable() {

        System.out.println("Button Pressed");
        colKey.setCellValueFactory(new PropertyValueFactory<KIWIDataModel, Integer>("Key"));

        Table.getItems().setAll(new StudentInfo().getAllstudentInfo());
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }
}

这是数据模型类:

import javafx.beans.property.SimpleIntegerProperty;

public class KIWIDataModel {

    public SimpleIntegerProperty firstName;

    public int getFirstName() {
        return firstName.get();
    }

    public void setFirstName(int fName) {
        firstName.set(fName);
    }
}

这是通过 JavaFx 场景生成器生成的 FXML 脚本:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="700.0" xmlns:fx="http://javafx.com/fxml" fx:controller="wakiliproject.SampleController">
  <children>
    <TableView fx:id="Table" prefHeight="400.0" prefWidth="700.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <columns>
        <TableColumn prefWidth="75.0" text="Column X" fx:id="colKey" />
      </columns>
    </TableView>
  </children>
</AnchorPane>

更新:这是我收到的错误:

Button Pressed
    [1]
    [1, First KIWI]
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
        at javafx.event.Event.fireEvent(Event.java:171)
        at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3117)
        at javafx.scene.Scene$ClickGenerator.access$8600(Scene.java:3055)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3337)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
        at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
        at com.sun.glass.ui.View.notifyMouse(View.java:922)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
        at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
        at java.lang.Thread.run(Thread.java:724)
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1437)
        ... 30 more
    Caused by: java.lang.NullPointerException
        at DB.KIWI.Try.KIWIDataModel.setFirstName(KIWIDataModel.java:14)
        at DB.KIWI.Try.StudentInfo.getAllstudentInfo(StudentInfo.java:49)
        at wakiliproject.SampleController.selectKIWITable(SampleController.java:49)
        ... 40 more

最佳答案

出现空指针是因为您从未将firstName设置为任何值。尝试:

public IntegerProperty firstName = 
    new SimpleIntegerProperty(0);

按数字命名是一种奇怪的数据模型。

关于java - 用数据库数据填充表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951447/

相关文章:

SQL 查询使用两个表显示最小平均值以及标题

mysql - 具有 3000 万条目的表速度很慢。优化MySQL还是改用mongodb?

php - mysql 不同的查询

mysql - 通过包含参数/计算进行 SQL 分组(从 Access 转换为 MySql)

java - 无法在 servlet init 方法中构建 session 工厂

php - 从两个表中选择而不重复所有值

sql - 为什么Oracle没有all_functions表?

java.util.MissingResourceException

java - 从 Java Action 监听器返回一个字符串

java - 使用 Spring-Java 测试 Oracle DB 表结构