Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将TableColumn文本与存储在ObservalArrayList中的对象StaffTimesheet的TaskDay值进行匹配或比较?_Java_Mysql_Sql_Javafx - Fatal编程技术网

Java 如何将TableColumn文本与存储在ObservalArrayList中的对象StaffTimesheet的TaskDay值进行匹配或比较?

Java 如何将TableColumn文本与存储在ObservalArrayList中的对象StaffTimesheet的TaskDay值进行匹配或比较?,java,mysql,sql,javafx,Java,Mysql,Sql,Javafx,有4个java文件-SargonVenture、TimesheetScene、StaffTimesheetAccessor和StaffTimesheet SargonVenture是启动该计划并显示时间表的主要计划,如下所示: StaffTimesheetAccessor是数据库连接器,它执行查询并将查询结果存储到StaffTimesheet的对象中,然后将该对象存储在ObservalArrayList()中 在TimesheetScene中,我想做的是(如果dayListColumn的Tab

有4个java文件-SargonVenture、TimesheetScene、StaffTimesheetAccessor和StaffTimesheet

SargonVenture是启动该计划并显示时间表的主要计划,如下所示:

StaffTimesheetAccessor是数据库连接器,它执行查询并将查询结果存储到StaffTimesheet的对象中,然后将该对象存储在ObservalArrayList()中

在TimesheetScene中,我想做的是(如果dayListColumn的TableColumn等于ObservalArrayList中存储的object StaffTimesheet的TaskDay值,那么它将在dayListColumn的相应列中显示TaskNumberOfHours)。错误部分在TimesheetScene文件中注释

代码如下

SargonVenture.java:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class SargonVenture extends Application {

private static Stage primaryStage;
private static Scene scene;

@Override
public void start(Stage primaryStage) throws Exception {
    SargonVenture.primaryStage = primaryStage;

    TimesheetScene timesheetScene = new TimesheetScene();

    scene = new Scene(timesheetScene);

    primaryStage.setTitle("");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public void setStage(Stage stage)
{
    primaryStage = stage;
}

public static Stage getStage()
{
    return primaryStage;
}

public static void setNewScene(Scene scene)
{
    SargonVenture.scene = scene;
}

public static Scene getTheScene()
{
    return scene;
}

public static void main(String[] args) {
    launch(args);
}
}
TimesheetScene.java:

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;


public class TimesheetScene extends BorderPane{

private final TableView<StaffTimesheet> tableView = new TableView<>();

private final ComboBox<String> comboBoxStaffName;
private final ComboBox<String> comboBoxMonth;
private final ComboBox<String> comboBoxYear;
Button buttonSearch;

private final StaffTimesheetAccessor dataTimesheetAccessor;

public TimesheetScene() throws SQLException, ClassNotFoundException{

    dataTimesheetAccessor = new StaffTimesheetAccessor("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost/rka?useTimezone=true&serverTimezone=UTC", "root", "291222");

    GridPane gridPane1 = new GridPane();
    gridPane1.setHgap(10);
    GridPane gridPane2 = new GridPane();
    gridPane2.setHgap(10);
    GridPane gridPane3 = new GridPane();
    gridPane3.setHgap(10);


    comboBoxStaffName = new ComboBox<>();
    comboBoxStaffName.getItems().addAll(dataTimesheetAccessor.getStaffName());
    gridPane1.add(new Label("Staff Name: "), 0, 0);
    gridPane1.add(comboBoxStaffName, 1, 0);


    comboBoxMonth = new ComboBox<>();
    comboBoxMonth.getItems().addAll("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
    comboBoxMonth.setValue("01");
    gridPane2.add(new Label("Month: "), 0, 0);
    gridPane2.add(comboBoxMonth, 1, 0);


    comboBoxYear = new ComboBox<>();
    comboBoxYear.getItems().addAll(dataTimesheetAccessor.getYear());
    gridPane3.add(new Label("Year: "), 0, 0);
    gridPane3.add(comboBoxYear, 1, 0);


    HBox hbox1 = new HBox(100);
    hbox1.getChildren().addAll(gridPane1, gridPane2, gridPane3);
    hbox1.setPadding(new Insets(0, 0, 0, 0));


    buttonSearch = new Button("Search");

    HBox hbox2 = new HBox(40);
    hbox2.getChildren().addAll(hbox1, buttonSearch);
    hbox2.setAlignment(Pos.CENTER);
    hbox2.setPadding(new Insets(20, 50, 20, 50));
    setTop(hbox2);


    TableColumn dayColumn = new TableColumn("Day");
    tableView.getColumns().addAll(dayColumn);


    TableColumn[] dayListColumn = new TableColumn[31];
    int x = 1;
    for(int i = 0; i < dayListColumn.length; i++)
    {

        String y = String.valueOf(x);
        dayListColumn[i] = new TableColumn(y);
        dayColumn.getColumns().add(dayListColumn[i]);

        // HERE IS THE ERROR
        if(dayListColumn[i].getText().equals(dataTimesheetAccessor.getStaffDayHourList().get(i).getTaskDay())){
            dayListColumn[i].setCellValueFactory(new PropertyValueFactory<>("TaskNumberOfHours"));
        }
        x=x+1;
    }

    buttonSearch.setOnAction(e ->{
        String staffName = comboBoxStaffName.getValue();
        String monthOfTaskDay = comboBoxMonth.getValue();
        String yearOfTaskDay = comboBoxYear.getValue();
        try {
            tableView.setItems(dataTimesheetAccessor.getStaffDayHour(staffName, monthOfTaskDay, yearOfTaskDay));
        } catch (SQLException ex) {
            Logger.getLogger(TimesheetScene.class.getName()).log(Level.SEVERE, null, ex);
        }
    });

    setCenter(tableView);
}

}

查询结果如下:

提示1,不要编写SQL注入易受攻击的代码。技巧2-将结果放在堆栈溢出上,而不是放在异地映像站点上。技巧3-与其进入ArrayList,不如编写一个查询直接获取您想要的结果。我不知道我是否同意@danblack third Tip。只是我的意见。我认为可以将结果放入
数组列表中。不过,在这一点上,我反对使用
可观察列表。我认为需要在设置项目的
类中创建
observebleList
。这样,如果您需要查询数据库,您可以设置当前
ObservableList
的数据,而不是设置新的
ObservableList
。提示1,不要编写SQL代码。技巧2-将结果放在堆栈溢出上,而不是放在异地映像站点上。技巧3-与其进入ArrayList,不如编写一个查询直接获取您想要的结果。我不知道我是否同意@danblack third Tip。只是我的意见。我认为可以将结果放入
数组列表中。不过,在这一点上,我反对使用
可观察列表。我认为需要在设置项目的
类中创建
observebleList
。这样,如果需要查询数据库,可以设置当前
observebleList
的数据,而不是设置新的
observebleList
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;


public class StaffTimesheetAccessor {

private final Connection connection;

ObservableList<StaffTimesheet> staffDayHourList;

public StaffTimesheetAccessor(String driverClassName, String dbURL, String user, String password) throws SQLException, ClassNotFoundException{
    Class.forName(driverClassName);
    connection = DriverManager.getConnection(dbURL, user, password);
}

public void shutdown() throws SQLException {
    if(connection != null)
    {
        connection.close();
    }
}

public ObservableList<StaffTimesheet> getStaffDayHour(String staffName, String monthOfTaskDay, String yearOfTaskDay) throws SQLException{
    String nameOfStaff = staffName;
    String month = monthOfTaskDay;
    String year = yearOfTaskDay;
    try(
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT DAY(Timesheet.TaskDay), ROUND((Timesheet.TaskNumberOfHours + (Timesheet.TaskNumberOfMinutes/60)), 2)\n" +
                                                        "FROM Clients\n" +
                                                        "INNER JOIN Timesheet ON Clients.ClientID = Timesheet.ClientID\n" +
                                                        "INNER JOIN Task ON Task.TaskID = Timesheet.TaskID\n" +
                                                        "INNER JOIN Staff ON Timesheet.StaffID = Staff.StaffID\n" +
                                                        "WHERE Staff.StaffName = '" + nameOfStaff + "'\n" +
                                                        "AND MONTH(Timesheet.TaskDay) = " + month + "\n" +
                                                        "AND YEAR(Timesheet.TaskDay) = " + year + ";");
    )
    {
        staffDayHourList = FXCollections.observableArrayList();
        while(resultSet.next()){
            String TaskDay = resultSet.getString(1);
            String TaskNumberOfHours = resultSet.getString(2);
            staffDayHourList.add(new StaffTimesheet(TaskDay, TaskNumberOfHours));
        }
        return staffDayHourList;
    }
}

public ObservableList<StaffTimesheet> getStaffDayHourList(){
    return staffDayHourList;
}



public ObservableList<String> getStaffName() throws SQLException{
    try(
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select StaffName from Staff");
    ){
        ObservableList<String> staffNameList = FXCollections.observableArrayList();
        while(resultSet.next()){
            String StaffName = resultSet.getString(1);
            staffNameList.add(StaffName);
        }
        return staffNameList;
    }
}

public ObservableList<String> getYear() throws SQLException{
    try(
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select DISTINCT(YEAR(TaskDay)) from Timesheet");
    ){
        ObservableList<String> timesheetYearList = FXCollections.observableArrayList();
        while(resultSet.next()){
            String timesheetYear = resultSet.getString(1);
            timesheetYearList.add(timesheetYear);
        }
        return timesheetYearList;
    }
}

}
import javafx.beans.property.SimpleStringProperty;


public class StaffTimesheet{

private SimpleStringProperty TaskDay;
private SimpleStringProperty TaskNumberOfHours;



public StaffTimesheet(String TaskDay, String TaskNumberOfHours){
    this.TaskDay = new SimpleStringProperty(TaskDay);
    this.TaskNumberOfHours = new SimpleStringProperty(TaskNumberOfHours);
}


public String getTaskDay(){
    return TaskDay.get();
}

public void setTaskDay(String TaskDay){
    this.TaskDay.set(TaskDay);
}

public String getTaskNumberOfHours(){
    return TaskNumberOfHours.get();
}

public void setTaskNumberOfHours(String TaskNumberOfHours){
    this.TaskNumberOfHours.set(TaskNumberOfHours);
}

}