JavaFX MySQL TableView如何选择行并将其添加到第二个TableView(并向DB添加值)?

时间:2019-01-20 20:39:03

标签: java mysql javafx

我试图找出如何在一个TableView中获取一行的值并将其添加到另一个TableView中(同时还将行值写入SQL文件)。基本上,我在左侧加载菜单,并希望将菜单项添加到右侧(顺序)。我在弄清楚如何处理“将商品添加到订单”事件时遇到很多麻烦。任何方向将不胜感激!

我无法弄清楚如何处理addItemToOrder()方法,因为似乎有些差异我无法解决。

package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;

public class Controller implements Initializable {

@FXML
private Label label;
@FXML
private TableView<MenuDetails> tableMenu;
@FXML
private TableColumn<MenuDetails, String> columnItemId;
@FXML
private TableColumn<MenuDetails, String> columnItemName;
@FXML
private TableColumn<MenuDetails, String> columnItemCost;
@FXML
private Button btnLoad;

@FXML
private TableView<OrderDetails> tableOrder;
@FXML
private TableColumn<OrderDetails, String> columnOrderNumber;
@FXML
private TableColumn<OrderDetails, String> columnItemNumber;
@FXML
private TableColumn<OrderDetails, String> columnItemNameOrder;
@FXML
private TableColumn<OrderDetails, String> columnItemCostOrder;
@FXML
private Button btnAdd;

/** Initialize observable list to hold database data */
private ObservableList<MenuDetails> data;
private ObservableList<OrderDetails> data2;
private Connection connection = null;
private PreparedStatement preparedStatement = null;

@Override
public void initialize(URL url, ResourceBundle rb) {
    connection = DbConnection.Connect();
}

@FXML
private void loadDataFromDatabase(ActionEvent event) {

    try {
        connection = DbConnection.Connect();
        data = FXCollections.observableArrayList();

        /** Execute query and store result in a resultSet */
        ResultSet rs = connection.createStatement().executeQuery(
                "SELECT * FROM menu;");

        while (rs.next()) {
            data.add(new MenuDetails(rs.getString("item_id"),
                    rs.getString("item_name"),
                    rs.getString("item_cost")));
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    /** Set cell value factory to tableview
     * NB.PropertyValue Factory must be the same */
    columnItemId.setCellValueFactory(new PropertyValueFactory<>("item_id"));
    columnItemName.setCellValueFactory(new PropertyValueFactory<>("item_name"));
    columnItemCost.setCellValueFactory(new PropertyValueFactory<>("item_cost"));

    tableMenu.setItems(null);
    tableMenu.setItems(data);

}
@FXML
private void addItemToOrder(ActionEvent event) throws SQLException {

    String sql = "INSERT INTO orders" +
            "(item_number,item_name,item_cost) " +
            "VALUES(?,?,?)";

    String itemNumber = columnItemId.getCellData(1);
    String itemName = columnItemName.getCellData(2);
    String itemCost = columnItemCost.getCellData(3);



    try {
        connection = DbConnection.Connect();
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, itemNumber);
        preparedStatement.setString(2, itemName);
        preparedStatement.setString(3, itemCost);


        int i = preparedStatement.executeUpdate();
        if (i == 1) {
            System.out.println("Data Insert Successful");
        }

        data2 = FXCollections.observableArrayList();

    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        preparedStatement.close();
    }

    /** Set cell value factory to tableview
     * NB.PropertyValue Factory must be the same */
    columnItemNumber.setCellValueFactory(new PropertyValueFactory<>("item_number"));
    columnItemNameOrder.setCellValueFactory(new PropertyValueFactory<>("item_name"));
    columnItemCostOrder.setCellValueFactory(new PropertyValueFactory<>("item_cost"));

    tableOrder.setItems(null);
    tableOrder.setItems(data2);

}

}

0 个答案:

没有答案