构造函数方法没有构造函数方法和错误时出错

时间:2016-12-02 15:29:37

标签: java sqlite javafx tableview

我的tableview有问题。每当我将一个项目添加到我的数据库时,它就会显示在我的tableview上,但只有在我关闭并重新打开tableview之后。但是我想在添加之后立即添加到我的tableview中。所以我做了一个刷新方法,但我从那里得到错误。我在代码中添加了注释,解释了问题发生的位置。

我已删除导入和@FXML标记

package packet;



public class DataControll implements Initializable {



    private static Connection con;
    private static Statement stat;
    private PreparedStatement prep;
    ResultSet rs = null;

    private ObservableList<Toidubaas> toidudata;

    public void eemaldaListist() {

    }

    public void lisaNupp(ActionEvent event) {

        try {
            String query = "INSERT OR IGNORE INTO Toiduinfo (Toidu, Kalorid, Valgud, Süsivesikud, Rasvad) VALUES(?, ?, ?, ?, ?)";
            prep = con.prepareStatement(query);

            prep.setString(1, lisaToit.getText());
            prep.setInt(2, Integer.parseInt(lisaKcal.getText()));
            prep.setInt(3, Integer.parseInt(lisaValk.getText()));
            prep.setInt(4, Integer.parseInt(lisaSysi.getText()));
            prep.setInt(5, Integer.parseInt(lisaRasv.getText()));
            prep.execute();
            prep.close();
        } catch (Exception er) {
            System.out.println(er.getMessage());
        }

        clearForm();
    }

    private void clearForm() {
        lisaToit.clear();
        lisaKcal.clear();
        lisaValk.clear();
        lisaSysi.clear();
        lisaRasv.clear();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        toidudata = FXCollections.observableArrayList();

        tbCal.setCellValueFactory(new PropertyValueFactory<Toidubaas, Integer>("rbCal"));
        tbProt.setCellValueFactory(new PropertyValueFactory<Toidubaas, Integer>("rbProt"));
        tbCarb.setCellValueFactory(new PropertyValueFactory<Toidubaas, Integer>("rbCarb"));
        tbFat.setCellValueFactory(new PropertyValueFactory<Toidubaas, Integer>("rbFat"));
        tbMeal.setCellValueFactory(new PropertyValueFactory<Toidubaas, String>("rbMeal"));



        try {

            con = DriverManager.getConnection("jdbc:sqlite:Fooditabel.sqlite");
            stat = con.createStatement();
            stat.executeUpdate(
                    "CREATE TABLE IF NOT EXISTS Toiduinfo (Toidu TEXT, Kalorid INTEGER, Valgud INTEGER, Süsivesikud INTEGER, Rasvad INTEGER)");

            ResultSet rs = con.createStatement()
                    .executeQuery("SELECT Toidu, Kalorid, Valgud, Süsivesikud, Rasvad FROM Toiduinfo");
            while (rs.next()) {
                Toidubaas nt = new Toidubaas(); //if i add constructor method, im getting error here, saying the constructor is undefined

                nt.rbMeal.set(rs.getString("Toidu"));
                nt.rbCal.set(rs.getInt("Kalorid"));
                nt.rbProt.set(rs.getInt("Valgud"));
                nt.rbCarb.set(rs.getInt("Süsivesikud"));
                nt.rbFat.set(rs.getInt("Rasvad"));
                toidudata.add(nt);
            }

            tbTabelview.setItems(toidudata);

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            ;
        }
    }

    public void refreshTable() {
        toidudata.clear();
        try {
            String query = "Select * FROM Toiduinfo";
            prep = con.prepareStatement(query);
            rs = prep.executeQuery();

            while (rs.next()) {
                toidudata.add(new Toidubaas(     //Getting error here, if im not using constructor method, saying the method is undefined.
                   rs.getString("Toidu"),
                   rs.getInt("Kalorid"), 
                   rs.getInt("Valgud"),
                   rs.getInt("Süsivesikud"), 
                   rs.getInt("Rasvad")));
                tbTabelview.setItems(toidudata);
            }
            prep.close();
        } catch (Exception e2) {

        }
    }
}

这是带有构造函数方法的类。请注意,构造函数方法已被注释掉,因此我的程序将使用数据库数据填充tableview。

public class Toidubaas {

    public  SimpleIntegerProperty rbCal = new SimpleIntegerProperty();
    public  SimpleIntegerProperty rbProt = new SimpleIntegerProperty();
    public  SimpleIntegerProperty rbCarb = new SimpleIntegerProperty();
    public  SimpleIntegerProperty rbFat = new SimpleIntegerProperty();
    public  SimpleStringProperty rbMeal = new SimpleStringProperty();

//This is the constructor method

      /*public Toidubaas(String sbMeal, Integer sbCal, Integer sbProt, Integer sbCarb, Integer sbFat) {

        this.rbMeal = new SimpleStringProperty(sbMeal);
        this.rbCal = new SimpleIntegerProperty(sbCal);
        this.rbProt = new SimpleIntegerProperty(sbProt);
        this.rbCarb = new SimpleIntegerProperty(sbCarb);
        this.rbFat = new SimpleIntegerProperty(sbFat);

    }*/

    public String getRbMeal() {
        return rbMeal.get();
    }

    public void setRbMeal(String v) {
        rbMeal.set(v);
    }

    public Integer getRbCal() {
        return rbCal.get();
    }

    public void setRbCal(Integer v) {
        rbCal.set(v);
    }

    public Integer getRbProt() {
        return rbProt.get();
    }

    public void setRbProt(Integer v) {
        rbProt.set(v);
    }

    public Integer getRbCarb() {
        return rbCarb.get();
    }

    public void setRbCarb(Integer v) {
        rbCarb.set(v);
    }

    public Integer getRbFat() {
        return rbFat.get();
    }

    public void setRbFat(Integer v) {
        rbFat.set(v);
    }



}

2 个答案:

答案 0 :(得分:0)

你在这里提出的问题实在令人困惑。我建议发布实际的错误文本,而不是留下代码内注释。

因此,您尝试以两种不同的方式使用Toidubaas

第一个(空构造函数)

Toidubaas nt = new Toidubaas(); //if i add constructor method, im getting error here, saying the constructor is undefined

nt.rbMeal.set(rs.getString("Toidu"));
nt.rbCal.set(rs.getInt("Kalorid"));
nt.rbProt.set(rs.getInt("Valgud"));
nt.rbCarb.set(rs.getInt("Süsivesikud"));
nt.rbFat.set(rs.getInt("Rasvad"));
toidudata.add(nt);

第二个(参数化构造函数)

toidudata.add(new Toidubaas(     //Getting error here, if im not using constructor method, saying the method is undefined.
rs.getString("Toidu"),
rs.getInt("Kalorid"), 
rs.getInt("Valgud"),
rs.getInt("Süsivesikud"), 
rs.getInt("Rasvad")));
tbTabelview.setItems(toidudata);

有两种方法可以解决这个问题:要么标准化你如何访问它(IE:将代码改为第一个或第二个,反之亦然),或支持这两种方法。

<强> Toidubaas

public class Toidubaas {

    public  SimpleIntegerProperty rbCal = new SimpleIntegerProperty();
    public  SimpleIntegerProperty rbProt = new SimpleIntegerProperty();
    public  SimpleIntegerProperty rbCarb = new SimpleIntegerProperty();
    public  SimpleIntegerProperty rbFat = new SimpleIntegerProperty();
    public  SimpleStringProperty rbMeal = new SimpleStringProperty();

    //Empty Constructor (only need this if standardized to first approach)
    public Toidubaas() {}

    //Parameterized Constructor (only need this if standardized to second approach) 
    public Toidubaas(String sbMeal, Integer sbCal, Integer sbProt, Integer sbCarb, Integer sbFat) {

        rbMeal.set(sbMeal);
        rbCal.set(sbCal);
        rbProt.set(sbProt);
        rbCarb.set(sbCarb);
        rbFat.set(sbFat);    
    }

    //Getters/Setters
    ...
}

答案 1 :(得分:0)

如果您不想添加带参数的构造函数,请通过调用setter逐个设置值。也可以重载构造函数(不应该多次创建属性)。

该行未添加到TableView,因为您永远不会添加它以防您成功提交数据,但这很容易修复:

String query = "INSERT OR IGNORE INTO Toiduinfo (Toidu, Kalorid, Valgud, Süsivesikud, Rasvad) VALUES(?, ?, ?, ?, ?)";

try (PreparedStatement prep = con.prepareStatement(query)) {
    String toidu = lisaToit.getText();
    int kalorid = Integer.parseInt(lisaKcal.getText());
    int valgud = Integer.parseInt(lisaValk.getText());
    int süsivesikud = Integer.parseInt(lisaSysi.getText());
    int rasvad = Integer.parseInt(lisaRasv.getText());

    prep.setString(1, toidu);
    prep.setInt(2, kalorid);
    prep.setInt(3, valgud);
    prep.setInt(4, süsivesikud);
    prep.setInt(5, rasvad);

    if (prep.executeUpdate() > 0) {
        // object filled with data from input data
        Toidubaas newToidubaas = new Toidubaas(toidu,
                                               kalorid,
                                               valgud,
                                               süsivesikud,
                                               rasvad);

        // add new item to table here
        toidudata.add(newToidubaas);
    }
    clearForm();
} catch (Exception er) {
    System.out.println(er.getMessage());
}