[英]Check existing entry in database + auto increment id -> multiple conditions
我正在創建一個帶有用戶表單的GUI應用程序來生成工作表,這些工作表保存在H2數據庫中,對於用戶表單,我手動輸入文本字段中的工作表編號和日期選擇器中的日期。
每張紙都有其ID,該ID在當月是唯一的。 該月的第一個創建工作表的ID為1,第二個為2,依此類推,但下個月ID應從1開始重新啟動。
我需要檢查數據庫中是否已存在該月份中的表單號。 我在數據庫中創建了另一個列,其中存儲了從文本字段中提取的數字+月份+年份,我在那里檢查它,但我正在尋找另一種方法而無需創建額外的列。
這是setter和getters(我只粘貼了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
}
我的問題是如何有效地檢查該月的數據庫中是否已存在工作表編號,而無需創建額外的列。 如果您認為實際解決方案是最佳解決方案,請告訴我,因為額外列是唯一唯一的列,因此當我需要從數據庫中刪除行時,我也使用該列。
另外我想不出自動增加工作表編號並將其設置為文本字段的解決方案,例如檢查當前月份的最后一個工作表編號並將其設置為“txt_SheetNumber”文本字段,如果沒有將其設置為1,那么每當我初始化應用程序以在文本字段中已經有工作表編號。 如果你可以幫助我,也將非常感激。
這是我第一次使用Java或任何其他編程語言,所以請隨時評論任何方面。 該應用程序對我來說只是為了讓我的工作更輕松。
經過一番挖掘,我找到了第二個問題的解決方案。 讓我知道我是否會改進它。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.