Java 代码>双精度: Prs.setDouble(3,Double.parseDouble(this.unitsInput1.getText());

Java 代码>双精度: Prs.setDouble(3,Double.parseDouble(this.unitsInput1.getText());,java,javafx,Java,Javafx,表中的Malt_Units列似乎是数字列,但您尝试将其作为字符串插入。您应该解析输入中的值,然后将其设置为Double: Prs.setDouble(3,Double.parseDouble(this.unitsInput1.getText()); Hi,欢迎来到StackOverflow。您的表结构是什么,可以为其提供DDL吗?使用ID1.setCellValueFactory(features->features.getValue().IDProperty())欢迎使用StackOverfl

表中的
Malt_Units
列似乎是数字列,但您尝试将其作为字符串插入。您应该解析输入中的值,然后将其设置为
Double

Prs.setDouble(3,Double.parseDouble(this.unitsInput1.getText());

Hi,欢迎来到StackOverflow。您的表结构是什么,可以为其提供DDL吗?使用
ID1.setCellValueFactory(features->features.getValue().IDProperty())
欢迎使用StackOverflow,而不是
PropertyValueFactory
Hi。您的表结构是什么,可以为其提供DDL吗?使用
ID1.setCellValueFactory(features->features.getValue().IDProperty())而不是
属性值工厂
感谢您的快速响应。不幸的是,我在专栏中仍然得到了0.0分。我需要更新StringConverter中的任何内容吗?我不久前使用此方法修复了该问题,我只是做了一些错误的事情。谢谢你的帮助!谢谢你的快速回复。不幸的是,我在专栏中仍然得到了0.0分。我需要更新StringConverter中的任何内容吗?我不久前使用此方法修复了该问题,我只是做了一些错误的事情。谢谢你的帮助!
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 primaryStage){
            try {
                Parent root = FXMLLoader.load(getClass().getResource("DatabaseView.fxml"));

                Scene scene = new Scene(root);

                primaryStage.setTitle("Brewer's Guide Inventory Management");

                primaryStage.setScene(scene);
                primaryStage.show();
            } catch (Exception e){
                e.printStackTrace();
            }
            }

        public static void main(String[] args) {
            launch(args);
        }
    }
    package sample;

    import javafx.beans.property.DoubleProperty;
    import javafx.beans.property.SimpleDoubleProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;

    public class beerDatabaseData {

        private final StringProperty ID;
        private final StringProperty name;
        private DoubleProperty units;

        public beerDatabaseData (String ID, String Name, Double Units)
        {
            this.ID = new SimpleStringProperty(ID);
            this.name =  new SimpleStringProperty(Name);
            this.units = new SimpleDoubleProperty(Units);
        }

        public StringProperty IDProperty()
        {
            return this.ID;
        }

        public String getID()
        {
            return this.IDProperty().get();
        }

        public void setID(final String ID)
        {
            this.IDProperty().set(ID);
        }

        public StringProperty nameProperty()
        {
            return this.name;
        }

        public String getName()
        {
            return this.nameProperty().get();
        }

        public void setName(final String name)
        {
            this.nameProperty().set(name);
        }

        public DoubleProperty unitsProperty()
        {
            return this.units;
        }

        public Double getUnits()
        {
            return this.unitsProperty().get();
        }

        public void setUnits(Double units)
        {
            this.unitsProperty().set(units);
        }
    }
package sample;

/*
Add method to auto-load data rather than pressing button (possible on-load).
Look at dependency injection (inversion of control (if possible) for testing).
Malt service (all actions with malt data).
Separation of concerns.
 */

//https://stackoverflow.com/questions/50358299/javafx-populating-multiple-tables-in-separate-tabs
//https://stackoverflow.com/questions/41465181/tableview-update-database-on-edit
//https://stackoverflow.com/questions/45977390/how-to-force-a-double-input-in-a-textfield-in-javafx

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class DatabaseViewController {
    @FXML
    private TextField idNumberInput1, idNumberInput2;

    @FXML
    private TextField nameInput1, nameInput2;

    @FXML
    private TextField unitsInput1, unitsInput2;

    @FXML
    private TableView<beerDatabaseData> beerTab1, beerTab2;

    @FXML
    private TableColumn<beerDatabaseData, String> ID1, ID2;

    @FXML
    private TableColumn<beerDatabaseData, String> name1, name2;

    @FXML
    private TableColumn<beerDatabaseData, Double> units1, units2;

    /*private ObservableList<DatabaseData> data;*/

    public void initialize(URL location, ResourceBundle resource) {
        //
    }

    private ObservableList<beerDatabaseData> data1;
    private ObservableList<beerDatabaseData> data2;

    public void LoadData(ActionEvent event) throws SQLException {
        try {
            Connection conn = SQLConnection.Connector();
            this.data1 = FXCollections.observableArrayList();
            this.data2 = FXCollections.observableArrayList();

            ResultSet rs1 = conn.createStatement().executeQuery("SELECT * FROM Malts");
            while (rs1.next()) {
                this.data1.add(new beerDatabaseData(rs1.getString(1), rs1.getString(2), rs1.getDouble(3)));
            }
            ResultSet rs2 = conn.createStatement().executeQuery("SELECT * FROM HOPS");
            while (rs2.next()) {
                this.data2.add(new beerDatabaseData(rs2.getString(1), rs2.getString(2), rs2.getDouble(3)));
            }

        }catch (SQLException e) {
            System.out.println(e);
        }

        this.ID1.setCellValueFactory(new PropertyValueFactory<beerDatabaseData, String>("ID"));
        this.name1.setCellValueFactory(new PropertyValueFactory<beerDatabaseData, String>("Name"));
        this.units1.setCellValueFactory(new PropertyValueFactory<beerDatabaseData, Double>("Units"));

        this.beerTab1.setItems(null);
        this.beerTab1.setItems(this.data1);

        this.ID2.setCellValueFactory(new PropertyValueFactory<beerDatabaseData, String>("ID"));
        this.name2.setCellValueFactory(new PropertyValueFactory<beerDatabaseData, String>("Name"));
        this.units2.setCellValueFactory(new PropertyValueFactory<beerDatabaseData, Double>("Units"));

        this.beerTab2.setItems(null);
        this.beerTab2.setItems(this.data2);
    }

    //Separating add functions for each ingredient since things are a little different for each.
    public void addMalt(ActionEvent event){
        try
        {
            Connection conn = SQLConnection.Connector();
            PreparedStatement Prs = conn.prepareStatement("INSERT INTO Malts(Malt_ID,Malt_Name, Malt_Units) VALUES(?, ?, ?)");

            Prs.setString(1,this.idNumberInput1.getText());
            Prs.setString(2, this.nameInput1.getText());

           /* Pattern validEditingState = Pattern.compile("-?(([1-9][0-9]*)|0)?(\\.[0-9]*)?");

            UnaryOperator<TextFormatter.Change> filter = c -> {
                String text = c.getControlNewText();
                if(validEditingState.matcher(text).matches()){
                    return c;
                }
                else{
                    return null;
                }
            };

            StringConverter<Double> converter = new StringConverter<Double>() {
                @Override
                public Double fromString(String s) {
                    if (s.isEmpty() || "-".equals(s) || ".".equals(s) || "-.".equals(s)){
                        return 0.0;
                    } else{
                        return Double.valueOf(s);
                    }
                }

                @Override
                public String toString(Double d) {
                    return d.toString();
                }
            };

            TextFormatter<Double>  textFormatter = new TextFormatter<>(converter, 0.0, filter);
            unitsInput1.setTextFormatter(textFormatter);
*/
            Prs.setString(3, this.unitsInput1.getText());

            Prs.execute();

            conn.close();
        }
        catch(SQLException e)
        {
            System.out.println(e);
        }
    }

/*    public void addHops(ActionEvent event){
        try
        {
            Connection conn = SQLConnection.Connector();

        }
        catch(SQLException e){
            System.out.println(e);
        }
    }*/
}