检查数据库中的现有条目+自动递增ID->多个条件

时间:2019-04-01 12:17:14

标签: java javafx jdbc

我正在使用用户表单创建GUI应用程序以生成工作表,这些工作表保存在H2数据库中,至于我在用户字段中手动输入工作表编号和日期选择器中的日期的用户表单。

每张纸都有其ID,该ID在该月应是唯一的。该月创建的第一个工作表的ID为1,第二个工作表的ID为2,依此类推,但对于下个月,ID应该从1重新开始。

我需要检查该月份中该表号是否已存在于数据库中。我在数据库中创建了另一列,其中存储了我从文本字段中提取的数字+月+年,并在此处进行了检查,但是我在寻找另一种方法而不必创建额外的列。

这里是设置方法和获取方法(我只粘贴了3个值):

public class FiseDetails {

    private final IntegerProperty sheetNumber;
    private final ObjectProperty<Date> sheetDate;
    private final StringProperty numberAndDate;

public FiseDetails(Integer sheetNumber, Date sheetDate, String numberAndDate) {
        this.sheetNumber = new SimpleIntegerProperty(sheetNumber);
        this.sheetDate = new SimpleObjectProperty<>(sheetDate);
        this.numberAndDate = new SimpleStringProperty(numberAndDate);
}

    public Integer getSheetNumber() {
        return sheetNumber.get();
    }

    public void setSheetNumber(Integer value) {
        sheetNumber.set(value);
    }

    public IntegerProperty SheetNumberProperty() {
        return sheetNumber;
    }

    public Date getSheetDate() {
        return sheetDate.get();
    }

    public void setSheetDate(Date value) {
        sheetDate.set(value);
    }

    public ObjectProperty SheetDateProperty() {
        return sheetDate;
    }

    public String getNumberAndDate() {
        return numberAndDate.get();
    }

    public void setNumberAndDate(String value) {
        numberAndDate.set(value);
    }

    public StringProperty NumberAndDateProperty() {
        return numberAndDate;
    }

}

这是控制器的一部分:

public class StartGUIController implements Initializable {

    Connection conn = DbConnection.Connect();
    ObservableList<FiseDetails> data = FXCollections.observableArrayList();
    PreparedStatement preparedStatement = null;
    ResultSet rs = null;

    @FXML
    private TextField txt_SheetNumber;

    @FXML
    private DatePicker sheetDate;

@FXML
    void generateSheet(ActionEvent event) throws SQLException {

        Integer id = Integer.parseInt(txt_SheetNumber.getText());
        LocalDate selectedDate = sheetDate.getValue();
        Date date = Date.valueOf(selectedDate);
        LocalDate strgDate = sheetDate.getValue();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM- yyyy");
        String formatedDate = (strgDate).format(formatter);
        String last7 = formatedDate.substring(formatedDate.length() - 7);
        String numberAndDate = txt_SheetNumber.getText() + " " + last7;

try (PreparedStatement checkNumberAndDateExists = conn.prepareStatement("SELECT 1 FROM SHEETSDB WHERE NUMBERANDDATE = ?")) {
            checkNumberAndDateExists.setString(1, numberAndDate);
            try (ResultSet result = checkNumberAndDateExists.executeQuery()) {
                if (result.next()) {
                    Alert confirmation = new Alert(AlertType.INFORMATION);
                    confirmation.setTitle("Fisa existenta");
                    confirmation.setHeaderText(null);
                    confirmation.setContentText("Fisa " + txt_SheetNumber.getText() + "/" + sheetDate.getValue() + " exista");
                    confirmation.showAndWait();

                } else {
                    String query = "INSERT INTO SHEETSDB (ID, SHEETDATE,  NUMBERANDDATE) VALUES (?,?,?)";
                    preparedStatement = null;
                    try {
                        preparedStatement = conn.prepareStatement(query);
                        preparedStatement.setInt(1, id);
                        preparedStatement.setDate(2, date);
                        preparedStatement.setString(26, numberAndDate);

                    } catch (SQLException e) {
                        System.out.println(e);
                    } finally {
                        preparedStatement.execute();
                        preparedStatement.close();
                    }

                    Alert confirmation = new Alert(AlertType.INFORMATION);
                    confirmation.setTitle("Finalizare generare fisa");
                    confirmation.setHeaderText(null);
                    confirmation.setContentText("Fisa " + txt_SheetNumber.getText() + "/" + sheetDate.getValue() + " a fost creata cu succes");
                    confirmation.showAndWait();
                    TableData();
                }
            }
        }

    }

@Override
    public void initialize(URL url, ResourceBundle rb) {
//here I have some code that does not have any impact on my problem
     }
  1. 我的问题是如何有效地检查该月数据库中是否已经存在工作表编号,而不必创建额外的列。让我知道您是否认为实际的解决方案是最好的解决方案,因为额外的列是唯一的唯一列,因此当我需要从数据库中删除行时,我也会使用该列。

    < / li>
  2. 我也无法想到一种自动增加工作表编号并将其设置为文本字段的解决方案,例如检查当前月份的最后一个工作表编号并将其设置为“ txt_SheetNumber”文本字段(如果未设置)它设置为1,所以每当我初始化应用程序时,文本字段中已经有工作表编号。如果您能帮助我,也将不胜感激。

这是我第一次使用Java或任何其他编程语言,所以请随时评论任何方面。该应用程序对我来说只是为了使我的工作更轻松。

1 个答案:

答案 0 :(得分:0)

经过一番挖掘,我找到了第二个问题的解决方案。让我知道是否可以改善它。

int lastId() {

    int newSheetNumber=0;
    String query = "SELECT MAX(ID) FROM SHEETSDB WHERE MONTH(`SHEETDATE`)=MONTH(NOW()) AND YEAR(`SHEETDATE`)=YEAR(NOW())";
    try {
        preparedStatement = conn.prepareStatement(query);

        rs = preparedStatement.executeQuery();
        if (!rs.next()) {
            newSheetNumber = 1;
        }else {
            newSheetNumber = rs.getInt(1) + 1;
        }
        preparedStatement.close();
        rs.close();
        //System.out.println("The new sheet is: " + newSheetNumber);
    } catch (SQLException ex) {
        System.err.println("Error " + ex);
    }
    return newSheetNumber;
}