JavaFX无法在数据库列中添加数字

时间:2019-03-05 14:54:34

标签: java javafx

如果这是重复的话,我表示歉意,但是我还找不到答案。

我正在一个项目中运行JavaFX中的基本库存管理系统。我目前正在尝试允许用户将新项目添加到SQLite数据库中,而ID和项目名称恰好添加到数据库中时,输入文本字段中的初始值似乎会被忽略。

程序将可用单位字段中的值设置为0.0。这是我当前的代码:

Main.java

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);
        }
    }

beerDatabaseData.java

    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);
        }
    }

DatabaseViewController

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);
        }
    }*/
}

我想这是一个引人注目的问题,但我没有看到。要进行这项工作,我需要更改或包含哪些内容?谢谢你。

1 个答案:

答案 0 :(得分:0)

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

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