我一直在尝试使用从数据库中查询的数据加载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;
}
}
这是控制器类:
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
答案 0 :(得分:1)
出现空指针,因为您从未将firstName设置为任何内容。尝试:
public IntegerProperty firstName =
new SimpleIntegerProperty(0);
数字名称是一种奇怪的数据模型。