JavaFX无法向数据库列添加数字

问题描述 投票:1回答:1

如果这是重复的,我很抱歉,但我还没有找到答案。

我正在研究一个在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);
        }
    }

beer database data.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);
        }
    }*/
}

我想这引起了一个明显的问题,但我没有看到它。我需要更改或包含哪些才能使其工作?先谢谢你。

java javafx
1个回答
0
投票

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

Prs.setDouble(3, Double.parseDouble(this.unitsInput1.getText()));
© www.soinside.com 2019 - 2024. All rights reserved.