JavaFX gui - 无法保存数据

时间:2018-01-10 18:45:19

标签: sqlite user-interface javafx tableview

我需要让gui用自动增量ID显示数据库中的所有对象。当我们点击某个对象,然后点击编辑按钮,我们可以编辑名称等数据。当我们结束更改我们的对象时,我们必须点击需要更新数据的保存按钮。我如何将自动增量ID添加到数据库? (我已创建一个,但没有ID)当我向数据库添加一个id时,我无法在receiveCats.fxml上显示任何数据。我的第二个问题是这个保存按钮。我如何将编辑过的数据推送到数据库中的对象?

数据库类

package Database;

import Model.Cats;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;


public class Database {

public static final String Driver = "org.sqlite.JDBC";
public static final String DB_url = "jdbc:sqlite:DB/ShelterDB.db/";

private Connection connection;
private Statement statement;

public Database() {

    try {
        Class.forName(Database.Driver);
    } catch (ClassNotFoundException e) {
        System.out.println("No driver JDBC");
        e.printStackTrace();
    }

    try {
        connection = DriverManager.getConnection(DB_url);
        statement = connection.createStatement();
    } catch (SQLException e) {
        System.out.println("Problem with opening the connection");
        e.printStackTrace();
    }
    createTables();
}

public boolean createTables() {
    String createCats = "CREATE TABLE IF NOT EXISTS Cats (Cat_id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(255), race varchar(255), gender varchar(255), coat_color varchar(255),age int)";
    String createDogs = "CREATE TABLE IF NOT EXISTS Dogs (Dog_id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(255), race varchar(255), gender varchar(255), coat_color varchar(255),age int)";
    String createManagments = "CREATE TABLE IF NOT EXISTS Managment (Managment_id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), surname varchar(255), username varchar(255), password varchar(255), telephone_number int)";
    String createEmployes = "CREATE TABLE IF NOT EXISTS Employes (Employe_id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), surname varchar(255), age int, city varchar(255), street varchar(255), house_number int, education varchar(255), telephone_number int, sallary double)";
    String createStatus = "CREATE TABLE IF NOT EXISTS Status (foodForCats int, foodForDogs int, water int, equipment int, placesForCats int, placesForDogs int)";

    try {
        statement.execute(createCats);
        statement.execute(createDogs);
        statement.execute(createManagments);
        statement.execute(createEmployes);
        statement.execute(createStatus);
    } catch (SQLException e) {
        System.out.println("Problem with creating a table");
        e.printStackTrace();
        return false;
    }
    return true;
}

public boolean insertCat(int id,String name, String race, String gender, String coat_color, int age) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into Cats values (NULL,?,?,?,?,?);");
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, race);
        preparedStatement.setString(3, gender);
        preparedStatement.setString(4, coat_color);
        preparedStatement.setInt(5, age);
        preparedStatement.execute();
    } catch (SQLException e) {
        System.out.println("Error with insert an cat to the table");
        e.printStackTrace();
        return false;
    }
    return true;
}

public boolean insertDog(String name, String race, String gender, String coat_color, int age) {

    try {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into Dogs values (NULL,?,?,?,?,?);");
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, race);
        preparedStatement.setString(3, gender);
        preparedStatement.setString(4, coat_color);
        preparedStatement.setInt(5, age);
        preparedStatement.execute();
    } catch (SQLException e) {
        System.out.println("Error with insert an dog to the table");
        e.printStackTrace();
        return false;
    }
    return true;
}

public boolean insertManagment(String name, String surname, String username, String password, int telephone_number) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into Managment values (NULL,?,?,?,?,?);");
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, surname);
        preparedStatement.setString(3, username);
        preparedStatement.setString(4, password);
        preparedStatement.setInt(5, telephone_number);
        preparedStatement.execute();
    } catch (SQLException e) {
        System.out.println("Error with insert an managment to the table");
        e.printStackTrace();
        return false;
    }
    return true;
}

public boolean insertEmployes(String name, String surname, int age, String city, String street, int house_number, String education, int telephone_number, double sallary) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into Employees values (NULL,?,?,?,?,?,?,?,?,?);");
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,surname);
        preparedStatement.setInt(3,age);
        preparedStatement.setString(4,city);
        preparedStatement.setString(5,street);
        preparedStatement.setInt(6,house_number);
        preparedStatement.setString(7,education);
        preparedStatement.setInt(8,telephone_number);
        preparedStatement.setDouble(9,sallary);
        preparedStatement.execute();
    } catch (SQLException e) {
        System.out.println("Error with insert an employee to the table");
        e.printStackTrace();
        return false;
    }
    return true;
}


public boolean insertStatus(int foodForCats, int foodForDogs, int water, int equipment, int placesForCats, int placesForDogs){
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into status values (?,?,?,?,?,?);");
        preparedStatement.setInt(1,foodForCats);
        preparedStatement.setInt(2,foodForDogs);
        preparedStatement.setInt(3,water);
        preparedStatement.setInt(4,equipment);
        preparedStatement.setInt(5,placesForCats);
        preparedStatement.setInt(6,placesForDogs);
        preparedStatement.execute();
    }catch (SQLException e){
        System.out.println("Error with insert values into Status");
        e.printStackTrace();
        return false;
    }
    return true;
}

public boolean updateCats(Cats cats){
    try {
        String updateData = "UPDATE Cats SET name=?, race=?, gender=?, coat_color=?, age=?";
        PreparedStatement preparedStatement = connection.prepareStatement(updateData);
        preparedStatement.setString(1,cats.getName());
        preparedStatement.setString(2,cats.getRace());
        preparedStatement.setString(3,cats.getGender());
        preparedStatement.setString(4,cats.getCoatColor());
        preparedStatement.setInt(5,cats.getAge());
        int res = preparedStatement.executeUpdate();
        return  (res>0);
    }catch (SQLException e){
        System.out.println("Can't update data\n"+e.getMessage());
    }
    return false;
}

public ResultSet execQuery(String query){
    ResultSet resultSet;
    try {
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
    }catch (SQLException e){
        System.out.println("Error in exec query\n"+e.getMessage());
        return null;
    }finally {
    }
    return resultSet;
}

public boolean execAction(String qu){
    try {
        statement = connection.createStatement();
        statement.execute(qu);
        return true;
    }catch (SQLException e){
        System.out.println("excecption at exec action\n"+e.getMessage());
        return false;
    }finally {

    }
}

public void closeConnection() {
    try {
        connection.close();
    } catch (SQLException e) {
        System.err.println("Error with shutdown");
        e.printStackTrace();
    }
}

}

猫模型类

package Model;



import javafx.beans.property.*;

public class Cats {


public Cats(Integer id,String name, String race, String gender, String coatColor, Integer age) {
    this.id = new SimpleIntegerProperty(id);
    this.name = new SimpleStringProperty(name);
    this.race = new SimpleStringProperty(race);
    this.gender = new SimpleStringProperty(gender);
    this.coatColor = new SimpleStringProperty(coatColor);
    this.age = new SimpleIntegerProperty(age);
}

public IntegerProperty id;
public IntegerProperty IDproperty(){return id;}
public Integer getID(){return IDproperty().get();}

public StringProperty name;

public void setName(String value) {
    nameProperty().set(value);
}

public StringProperty nameProperty() {
    return name;
}

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

public StringProperty race;

public void setRace(String value) {
    raceProperty().set(value);
}

public StringProperty raceProperty() {
    return race;
}

public String getRace() {
    return raceProperty().get();
}

public StringProperty gender;

public void setGender(String value) {
    genderProperty().set(value);
}

public StringProperty genderProperty() {
    return gender;
}

public String getGender() {
    return genderProperty().get();
}


public StringProperty coatColor;

public void setCoatColor(String value) {
    coatColorProperty().set(value);
}

public StringProperty coatColorProperty() {
    return coatColor;
}

public String getCoatColor() {
    return coatColorProperty().get();
}


public IntegerProperty age;

public void setAge(Integer value) {
    ageProperty().set(value);
}

public IntegerProperty ageProperty() {
    return age;
}

public Integer getAge() {
    return ageProperty().get();
}

}

检索cat控制器

package Animals.Cats.retrieveCats;

import Animals.Cats.editCat.editCatController;
import Database.Database;

import Model.Cats;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;


public class retrieveCatsController implements Initializable {

    ObservableList<Cats> list = FXCollections.observableArrayList();

    @FXML
    private AnchorPane tableRootPanel; //?

    @FXML
    private TableView<Cats> tableView;

    @FXML
    private TableColumn<Cats,Integer> catsId;

    @FXML
    private TableColumn<Cats, String> catsName;

    @FXML
    private TableColumn<Cats, String> catsRace;

    @FXML
    private TableColumn<Cats, String> catsGender;

    @FXML
    private TableColumn<Cats, String> catsCoatColor;

    @FXML
    private TableColumn<Cats, Integer> catsAge;

    public void editCats(ActionEvent actionEvent) {

        Cats editSelectedCat = tableView.getSelectionModel().getSelectedItem();
        if (editSelectedCat == null) {
            System.out.println("You have to select object that you want to edit");
            return;
        }
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/Animals/Cats/editCat/editCat.fxml"));
            Parent editCatParent = loader.load();
            Scene editCatScene = new Scene(editCatParent);
            Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
            stage.setScene(editCatScene);
            stage.show();
        } catch (IOException e) {
            System.out.println("can't load an edit window.\n" + e.getMessage());
        }
    }

    public void deleteSelectedCat(ActionEvent actionEvent) {
        ObservableList<Cats> selectedCat, list;

        list = tableView.getItems();
        selectedCat = tableView.getSelectionModel().getSelectedItems();

        selectedCat.forEach(list::remove);
    }

    public void goToMainMenuCats(ActionEvent actionEvent) throws IOException {
        Parent animalsMainMenuParent = FXMLLoader.load(getClass().getResource("/Animals/Cats/mainMenuCats/Cats.fxml"));
        Scene animalsMainMenuScene = new Scene(animalsMainMenuParent);
        Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
        stage.setScene(animalsMainMenuScene);
        stage.show();
    }

    public void goToEmployees(ActionEvent actionEvent) throws IOException {
        Parent employeesMainMenuParent = FXMLLoader.load(getClass().getResource("/Employees/mainMenu/employeesMain.fxml"));
        Scene mainMenuScene = new Scene(employeesMainMenuParent);
        Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
        stage.setScene(mainMenuScene);
        stage.show();
    }

    public void goToAnimals(ActionEvent actionEvent) throws IOException {
        Parent goToAnimalsMenuParent = FXMLLoader.load(getClass().getResource("/Animals/mainMenu/animalsMainMenu.fxml"));
        Scene goToAnimalsMenuScene = new Scene(goToAnimalsMenuParent);
        Stage goToAnimalsMenuStage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
        goToAnimalsMenuStage.setScene(goToAnimalsMenuScene);
        goToAnimalsMenuStage.show();
    }

    public void goToStatus(ActionEvent actionEvent) throws IOException {
        Parent goToStatusMenuParent = FXMLLoader.load(getClass().getResource("/Status/Menu/statusMainMenu.fxml"));
        Scene goToStatusMenuScene = new Scene(goToStatusMenuParent);
        Stage goToStatusMenuStage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
        goToStatusMenuStage.setScene(goToStatusMenuScene);
        goToStatusMenuStage.show();
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        initColumns();
        loadData();
    }


    private void initColumns() {
        catsId.setCellValueFactory(new PropertyValueFactory<>("ID"));
        catsName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        catsRace.setCellValueFactory(new PropertyValueFactory<>("Race"));
        catsGender.setCellValueFactory(new PropertyValueFactory<>("Gender"));
        catsCoatColor.setCellValueFactory(new PropertyValueFactory<>("coatColor"));
        catsAge.setCellValueFactory(new PropertyValueFactory<>("Age"));
    }

    private void loadData() {
        Database database = new Database();
        String query = "SELECT * FROM Cats";
        ResultSet resultSet = database.execQuery(query);
        try {
            while (resultSet.next()) {

                Integer ID = resultSet.getInt("ID");
                String name = resultSet.getString("name");
                String race = resultSet.getString("race");
                String gender = resultSet.getString("gender");
                String coatColor = resultSet.getString("coat_color");
                Integer age = resultSet.getInt("age");

                list.add(new Cats(ID,name, race, gender, coatColor, age));
            }
        } catch (SQLException e) {
            System.out.println("Can't select data from Cats\n" + e.getMessage());
        }
        tableView.getItems().setAll(list);
    }
}

编辑cat控制器

package Animals.Cats.editCat;

import Animals.Cats.addCats.addCatController;
import Animals.Cats.retrieveCats.retrieveCatsController;
import Database.Database;
import Model.Cats;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;


import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;


public class editCatController implements Initializable{

private  ObservableList<Cats> list = FXCollections.observableArrayList();
private  Database database;

    @FXML
    private TableView<Cats> table;

    @FXML
    private TableColumn<Cats,Integer> idCol;

    @FXML
    private TableColumn<Cats,String> nameCol;

    @FXML
    private TableColumn<Cats,String> raceCol;

    @FXML
    private TableColumn<Cats,String> genderCol;

    @FXML
    private TableColumn<Cats,String> coatColorCol;

    @FXML
    private TableColumn<Cats,Integer> ageCol;


//    public void Save(){
//        String updateData = "UPDATE Cats set name=?, race=?, gender=?, coat_color=?, age=?"
//    }


    private void loadData() {
        list.clear();
        Database database = new Database();
        String query = "SELECT * FROM Cats";
        ResultSet resultSet = database.execQuery(query);
        try {
            while (resultSet.next()) {
                Integer ID = resultSet.getInt("ID");
                String name = resultSet.getString("name");
                String race = resultSet.getString("race");
                String gender = resultSet.getString("gender");
                String coatColor = resultSet.getString("coat_color");
                Integer age = resultSet.getInt("age");

                list.add(new Cats(ID,name, race, gender, coatColor, age));
            }
        } catch (SQLException e) {
            System.out.println("Can't select data from Cats\n" + e.getMessage());
        }
        table.getItems().setAll(list);
    }

    public void changeName(TableColumn.CellEditEvent editEvent){

        Cats cats = table.getSelectionModel().getSelectedItem();

        cats.setName(editEvent.getNewValue().toString());

    }

    public void changeRace(TableColumn.CellEditEvent editEvent){

        Cats cats = table.getSelectionModel().getSelectedItem();

        cats.setRace(editEvent.getNewValue().toString());

    }

    public void changeGender(TableColumn.CellEditEvent editEvent){

        Cats cats = table.getSelectionModel().getSelectedItem();

        cats.setGender(editEvent.getNewValue().toString());

    }

    public void changeCoatColor(TableColumn.CellEditEvent editEvent){

        Cats cats = table.getSelectionModel().getSelectedItem();

        cats.setCoatColor(editEvent.getNewValue().toString());

    }

    public void changeAge(TableColumn.CellEditEvent editEvent){

        Cats cats = table.getSelectionModel().getSelectedItem();

        cats.setAge((Integer) editEvent.getNewValue());

    }

    private void initColumns() {
        idCol.setCellValueFactory(new PropertyValueFactory<>("ID"));
        nameCol.setCellValueFactory(new PropertyValueFactory<>("Name"));
        raceCol.setCellValueFactory(new PropertyValueFactory<>("Race"));
        genderCol.setCellValueFactory(new PropertyValueFactory<>("Gender"));
        coatColorCol.setCellValueFactory(new PropertyValueFactory<>("coatColor"));
        ageCol.setCellValueFactory(new PropertyValueFactory<>("Age"));
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        database = new Database();
        initColumns();
        loadData();

        table.setEditable(true);

        nameCol.setCellFactory(TextFieldTableCell.forTableColumn());
        raceCol.setCellFactory(TextFieldTableCell.forTableColumn());
        genderCol.setCellFactory(TextFieldTableCell.forTableColumn());
        coatColorCol.setCellFactory(TextFieldTableCell.forTableColumn());



    }

    public void test(ActionEvent actionEvent) throws IOException {
        Parent animalsMainMenuParent = FXMLLoader.load(getClass().getResource("/Animals/Cats/retrieveCats/retrieveCats.fxml"));
        Scene animalsMainMenuScene = new Scene(animalsMainMenuParent);
        Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
        stage.setScene(animalsMainMenuScene);
        stage.show();
    }

    public void refreshData(ActionEvent actionEvent) {
        loadData();
    }
}

检索cat fxml

    <?xml version="1.0" encoding="UTF-8"?>

    <?import com.jfoenix.controls.JFXButton?>
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.ButtonBar?>
    <?import javafx.scene.control.SplitPane?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.layout.AnchorPane?>

    <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Animals.Cats.retrieveCats.retrieveCatsController">
       <children>
          <SplitPane dividerPositions="0.29797979797979796" layoutY="14.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <items>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                   <children>
                      <JFXButton layoutX="62.0" layoutY="93.0" mnemonicParsing="false" onAction="#goToAnimals" text="Animals" />
                      <JFXButton layoutX="62.0" layoutY="118.0" mnemonicParsing="false" onAction="#goToEmployees" text="Employees" />
                      <JFXButton alignment="CENTER" layoutX="62.0" layoutY="143.0" mnemonicParsing="false" onAction="#goToStatus" text="Status" />
                   </children>
                   <padding>
                      <Insets left="10.0" top="20.0" />
                   </padding></AnchorPane>
              <AnchorPane fx:id="tableRootPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                   <children>
                      <ButtonBar layoutX="138.0" layoutY="345.0" prefHeight="40.0" prefWidth="190.0">
                        <buttons>
                            <JFXButton mnemonicParsing="false" onAction="#editCats" text="Edit" />
                            <JFXButton mnemonicParsing="false" onAction="#deleteSelectedCat" text="Delete" />
                            <!--<JFXButton mnemonicParsing="false" onAction="#goToMainMenuCats" text="Previous" />-->
                        </buttons>
                      </ButtonBar>
                      <TableView fx:id="tableView" prefHeight="345.0" prefWidth="417.0">
                        <columns>
                            <TableColumn fx:id="catsId" prefWidth="75.0" text="ID" />
                          <TableColumn fx:id="catsName" prefWidth="75.0" text="Name" />
                          <TableColumn fx:id="catsRace" prefWidth="75.0" text="Race" />
                          <TableColumn fx:id="catsGender" prefWidth="75.0" text="Gender" />
                          <TableColumn fx:id="catsCoatColor" prefWidth="75.0" text="Coat color" />
                          <TableColumn fx:id="catsAge" prefWidth="75.0" text="Age" />
                        </columns>
                         <columnResizePolicy>
                            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                         </columnResizePolicy>
                      </TableView>
                   </children>
                </AnchorPane>
            </items>
          </SplitPane>
       </children>
    </AnchorPane>


edit cat fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Animals.Cats.editCat.editCatController">
   <children>
      <TableView fx:id="table" layoutX="129.0" layoutY="14.0" prefHeight="327.0" prefWidth="457.0">
        <columns>
            <TableColumn fx:id="idCol"  prefWidth="75.0" text="ID" />
          <TableColumn fx:id="nameCol" onEditCommit="#changeName" prefWidth="75.0" text="Name" />
          <TableColumn fx:id="raceCol" onEditCommit="#changeRace" prefWidth="75.0" text="Race" />
          <TableColumn fx:id="genderCol" onEditCommit="#changeGender" prefWidth="75.0" text="Gender" />
          <TableColumn fx:id="coatColorCol" onEditCommit="#changeCoatColor" prefWidth="75.0" text="Coat color" />
          <TableColumn fx:id="ageCol" onEditCommit="#changeAge" prefWidth="75.0" text="Age" />
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
      <ButtonBar layoutX="372.0" layoutY="347.0" prefHeight="40.0" prefWidth="200.0">
        <buttons>
            <JFXButton mnemonicParsing="false" onAction="#Save" text="Save" />
            <JFXButton mnemonicParsing="false" text="Cencel" />
            <JFXButton mnemonicParsing="false" onAction="#refreshData" text="Refresh" />
        </buttons>
      </ButtonBar>
      <Button layoutX="262.0" layoutY="354.0" mnemonicParsing="false" onAction="#test" text="Button" />
   </children>
</AnchorPane>

我收到的错误:

无法从Cats中选择数据 没有这样的专栏:&#39; ID&#39;

2 个答案:

答案 0 :(得分:2)

这里有很多代码,你似乎在问多种问题。我只回答第一个问题(一般来说,你应该一次只能在一个问题中解决一个问题,你应该创建一个完整的例子,除了演示手头的问题外什么都不做。)

您的create语句定义了主键列Cat_id

String createCats = "CREATE TABLE IF NOT EXISTS Cats (Cat_id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(255), race varchar(255), gender varchar(255), coat_color varchar(255),age int)";

但是在处理查询时,您尝试从名为ID的列中检索值:

Integer ID = resultSet.getInt("ID");

你需要进行这些匹配,例如

Integer ID = resultSet.getInt("Cat_id");

答案 1 :(得分:-1)

确保您的数据库已启用自动增量。然后使用sql“INSERT命令”将数据插入数据库,稍后使用sql“SELECT命令”显示它。确保将sql命令链接到控制器中的gui按钮。