Mysql javafx中的条形图没有显示任何内容

Mysql javafx中的条形图没有显示任何内容,mysql,javafx,Mysql,Javafx,在workbench中,它给出了以下内容: 但是,当我打开fxml文件时,它不会在条形图中显示任何内容。 我的javafx或mysql语法有问题吗? 程序本身在运行时不会给出任何错误。 我甚至不知道从哪里开始解决这个问题。不应该是(y)值数字(vs.字符串)吗?最好使用适当的类型参数,然后编译器会警告您:)您指的是哪里?@FrankSeo您正在为数据的x值和y值添加Strings:currentYearSeries.getData().add(新的XYChart.data(resultSet

在workbench中,它给出了以下内容:

但是,当我打开fxml文件时,它不会在条形图中显示任何内容。

我的javafx或mysql语法有问题吗? 程序本身在运行时不会给出任何错误。
我甚至不知道从哪里开始解决这个问题。

不应该是(y)值数字(vs.字符串)吗?最好使用适当的类型参数,然后编译器会警告您:)您指的是哪里?@FrankSeo您正在为数据的x值和y值添加
String
s:
currentYearSeries.getData().add(新的XYChart.data(resultSet.getString(2),resultSet.getString(3))有关类型参数的注释参考
私有XYChart.Series currentYearSeries
。您的IDE肯定会在这里给您一个警告吗?您应该有
private XYChart.Series currentYearSeries
(可能还有其他类似的问题,例如
BarChart
应该是
BarChart
)。那么您应该会在上一条评论的行中看到一个错误,指出问题所在。@James\u感谢您的评论!我想我现在明白问题所在了。
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.BarChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="800.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Views.MonthlyHoursController">
   <children>
      <VBox alignment="CENTER" layoutX="121.0" layoutY="121.0" prefHeight="600.0" prefWidth="800.0" spacing="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Label styleClass="header" text="Monthly Hours" />
            <HBox prefHeight="292.0" prefWidth="760.0">
               <children>
                  <BarChart fx:id="barChart" prefHeight="292.0" prefWidth="755.0">
                    <xAxis>
                      <CategoryAxis fx:id="months" side="BOTTOM" />
                    </xAxis>
                    <yAxis>
                      <NumberAxis fx:id="hoursWorked" side="LEFT" />
                    </yAxis>
                  </BarChart>
               </children>
            </HBox>
            <HBox alignment="BOTTOM_RIGHT" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
               <children>
                  <Button mnemonicParsing="false" text=" Back" />
               </children>
            </HBox>
         </children>
         <padding>
            <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
         </padding>
      </VBox>
   </children>
</AnchorPane>
package Views;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

public class MonthlyHoursController implements Initializable {
    @FXML private BarChart<?, ?> barChart;
    @FXML private CategoryAxis months;
    @FXML private NumberAxis hoursWorked;

    private XYChart.Series currentYearSeries, previousYearSeries;


    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        currentYearSeries = new XYChart.Series<>();
        previousYearSeries = new XYChart.Series<>();

//      barChart.setTitle("Hours Worked");
        months.setLabel("Months");
        hoursWorked.setLabel("Hours Worked");

        currentYearSeries.setName(Integer.toString(LocalDate.now().getYear()));
        previousYearSeries.setName(Integer.toString(LocalDate.now().getYear()-1));
        try {
            populateSeriesFromDB();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        barChart.getData().addAll(previousYearSeries);
        barChart.getData().addAll(currentYearSeries);

    }

    private void populateSeriesFromDB() throws SQLException {
        Connection conn = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try{
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bakery?useSSL=false", "root",
                    "a3756421");
            statement = conn.createStatement();
            String sql = " Select Year(dateworked), monthname(dateworked), sum(hoursworked)" +
                            "From hoursworked" +
                            "Group by year(dateworked), month(dateworked)" +
                            "Order by Year(dateworked), month(dateworked);";
            resultSet = statement.executeQuery(sql);

            while (resultSet.next()){
                if(resultSet.getInt(1)==LocalDate.now().getYear())
                    currentYearSeries.getData().add(new XYChart.Data(resultSet.getString(2), resultSet.getString(3)));
                else if(resultSet.getInt(1)==LocalDate.now().getYear()-1)
                    previousYearSeries.getData().add(new XYChart.Data(resultSet.getString(2), resultSet.getString(3)));
            }
        }

        catch (SQLException e){
            System.err.println(e.getMessage());
        }
        finally{
            if(conn !=null)
                conn.close();
            if (statement != null)
                statement.close();
            if(resultSet != null)
                resultSet.close();
        }

    }

}
From hoursworked
Group by Year(dateworked), month(dateworked)
Order by Year(dateworked), month(dateworked);