遵循了有关dbms连接的视频教程,但该教程无法运行。
试图更改文件路径和Java版本
问题是我还有另一个JDBC项目,该项目工作正常,但很难将其连接到GUI
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 stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("calculator.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
从Scenebuilder分配变量
package sample;
public class ModelTable
{
String date, voltage, power, efficiency;
public ModelTable(String date, String voltage, String power,
String efficiency)
{
this.date = date;
this.voltage = voltage;
this.power = power;
this.efficiency = efficiency;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
public String getVoltage()
{
return voltage;
}
public void setVoltage(String voltage)
{
this.voltage = voltage;
}
public String getPower()
{
return power;
}
public void setPower(String power)
{
this.power = power;
}
public String getEfficiency()
{
return efficiency;
}
public void setEfficiency(String efficiency)
{
this.efficiency = efficiency;
}
}
应该显示sql的表的控制器
package sample;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import com.sun.istack.internal.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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 TableController implements Initializable
{
@FXML
private TableView<ModelTable> table;
@FXML
private TableColumn<ModelTable, String> col_Date;
@FXML
private TableColumn<ModelTable, String> col_Voltage;
@FXML
private TableColumn<ModelTable, String> col_Power;
@FXML
private TableColumn<ModelTable, String> col_Efficiency;
ObservableList<ModelTable> oblist = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
Connection con = DBConnector.getConnection();
ResultSet rs = con.createStatement().executeQuery("select * from readings");
while (rs.next()) {
oblist.add(new ModelTable(rs.getString("datetime"), rs.getString("voltageread"), rs.getString("powerread"), rs.getString("efficiency")));
}
}
catch (SQLException ex)
{
Logger.getLogger(TableController.class.getName(), null).log(Level.SEVERE, null, ex);
}
col_Date.setCellValueFactory(new PropertyValueFactory<>("Date"));
col_Voltage.setCellValueFactory(new PropertyValueFactory<>("Voltage"));
col_Power.setCellValueFactory(new PropertyValueFactory<>("Power"));
col_Efficiency.setCellValueFactory(new PropertyValueFactory<>("Efficiency"));
table.setItems(oblist);
}
}
数据库连接
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnector
{
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/SemesterProject4", "postgres", "******");
return connection;
}
}
在Scenebuilder中创建的FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" stylesheets="@style.css"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sample.TableController">
<children>
<ImageView layoutX="-41.0" layoutY="-29.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image
url="@../../../Java/src/GUI/Images/160928151119_1_900x600.jpg" />
</image>
</ImageView>
<DatePicker layoutX="14.0" layoutY="13.0" prefHeight="36.0"
prefWidth="251.0" AnchorPane.leftAnchor="10.0" />
<DatePicker layoutX="335.0" layoutY="13.0"
prefHeight="36.0" prefWidth="251.0" AnchorPane.rightAnchor="10.0" />
<TableView fx:id="table" layoutX="192.0" layoutY="112.0"
prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="10.0"
AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0"
AnchorPane.topAnchor="60.0">
<columns>
<TableColumn fx:id="col_Date" prefWidth="75.0"
text="Date" />
<TableColumn fx:id="col_Voltage" prefWidth="75.0"
text="Voltage" />
<TableColumn fx:id="col_Power" prefWidth="75.0"
text="Power" />
<TableColumn fx:id="col_Efficiency" prefWidth="75.0"
text="Efficiency" />
</columns>
</TableView>
</children>
</AnchorPane>
错误消息
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/Bragi/OneDrive%20-%20ViaUC/GBE%20VIA/Semester%204/SEP4%20Semester%20Project/DataBaseConnection/bin/sample/calculator.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at sample.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.NullPointerException
at com.sun.istack.internal.logging.Logger.getLogger(Unknown Source)
at sample.TableController.initialize(TableController.java:54)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Exception running application sample.Main