Java 如何更新房间数量?

Java 如何更新房间数量?,java,arrays,Java,Arrays,目前,我想在预订添加到数据库后更新我的房间可用性,但我现在面临的问题是,我不知道在预订后如何更新房间数量 我需要的解决方案是,当我输入房间数量并添加时,房间数据库将减去房间数量 DA房间 预约DA 预订面板添加按钮 使用roomAvailability=roomAvailability-1 添加新记录之前,请检查可用性是否大于0。如果大于零,分配一个房间。否则请分配另一个房间。在您的预订DA中,您正在插入新的预订,因此您还有房间DA,它会更新剩余房间的数量 roomDA.updateRoomQ

目前,我想在预订添加到数据库后更新我的房间可用性,但我现在面临的问题是,我不知道在预订后如何更新房间数量

我需要的解决方案是,当我输入房间数量并添加时,房间数据库将减去房间数量

DA房间 预约DA 预订面板添加按钮 使用roomAvailability=roomAvailability-1


添加新记录之前,请检查可用性是否大于0。如果大于零,分配一个房间。否则请分配另一个房间。

在您的预订DA中,您正在插入新的预订,因此您还有房间DA,它会更新剩余房间的数量


roomDA.updateRoomQuantity(roomId,amountFroomLeft);

roomId存在于您的bookingDA中唯一剩下的未知变量是AmountFroomLeft。因此,您应该编写一个新的方法来获得amounttofloomleft

比如:


public int getRoomQuantity(字符串roomID){
String sql=String.format(“从roomID='%s',roomID的房间中选择roomAvailability”);
试一试{
stmt=conn.createStatement();
ResultSet ResultSet=stmt.execute(sql);
resultset.next();
返回resultset.getInt(1);
}catch(SQLException-ex){
Logger.getLogger(RoomDA.class.getName()).log(Level.SEVERE,null,ex);
showMessageDialog(null,例如getMessage(),“ERROR”,JOptionPane.ERROR_MESSAGE);
}
}


你也应该自己做作业:)

任何人都可以帮我链接它。非常感谢你也许你可以在DB上添加触发器?如何?有什么办法可以添加触发器吗?最好是在同一事务中使用
addRecord()
updateRoomQuantity()
。我试图理解你的模型:在
房间
表中,
房间ID
如何与
房间左
关联?按照逻辑,你似乎可以预订几个房间。如果预订了两个房间,您是否打算调用两次
updateRoomQuantity
?。。。你能提供
房间
表的描述吗?我目前有2个数据库,1个用于预订,1个用于房间。因此,当我在预订面板中输入房间数量时,添加后,房间数据库中的房间数量将减少。我现在有这个问题。
public boolean updateRoomQuantity(String roomID, int amountOfRoomLeft){
    String sql = String.format("update room set roomAvailability =%d where roomID = '%s'", amountOfRoomLeft, roomID);
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
        return true;
    } catch (SQLException ex) {
        Logger.getLogger(RoomDA.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        return false;
    }
}
public boolean addRecord(String booID, String booDate, String booTime, String roomID, double roomPrice, int duration, String memberID,
        String receptionistID, String checkinStatus, double totalRoomPrice, double totalPrice) {
    String sql = String.format("insert into booking values('%s','%s','%s','%s',%.2f,%d,'%s','%s','%s',%.2f,%.2f)", booID, booDate, booTime, roomID, roomPrice, duration, memberID, receptionistID, checkinStatus, totalRoomPrice, totalPrice);
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
        return true;
    } catch (SQLException ex) {
        Logger.getLogger(BookingDA.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        return false;
    }
}
private void addBooking() {
    double total = 0.0;
    int roomQuantity= Integer.parseInt(jtfRoomQuantity.getText().trim());
    double price = Double.parseDouble(jtfRoomPrice.getText().trim());

    total = (roomQuantity* price);
    jtfTotalPrice.setText(String.valueOf(total));
    jtfTotalRoomPrice.setText(String.valueOf(total));
    if (!emptyFields()) {
        if (jcbAutoFillDate.isSelected()) {
            jbtFillInDate.doClick();
        }
        String booID = jtfBookingID.getText();
        String booDate = jtfBookingDate.getText();
        String booTime = jtfBookingTime.getText();
        String roomID = null;
        Room room = roomDA.getRecordByName(jtfAutoCompleteRoom.getText());
        double roomPrice = Double.parseDouble(jtfRoomPrice.getText());
        String memberID = null;
        Member member = memberDA.getRecordByName(jtfAutoCompleteMember.getText());
        String receptionistID = receptionistDA.getRecordByName(jcbAvailableReceptionist.getSelectedItem().toString()).getReceptionistID();
        String checkinStatus = jcbStatus.getSelectedItem().toString();
        double totalRoomPrice = Double.parseDouble(jtfTotalRoomPrice.getText());
        double totalPrice = Double.parseDouble(jtfTotalPrice.getText());

        memberID = member.getMemberID();
        roomID = room.getRoomID();

        if (bookingDA.addRecord(booID, booDate, booTime, roomID, roomPrice, duration, memberID, receptionistID, checkinStatus, totalRoomPrice, totalPrice)) {
            JOptionPane.showMessageDialog(null, "Successfully added");
            refreshTableContent();
            autoResizeTable();
            reset();
        }
    }
}
public boolean updateRoomQuantity(String roomID, int amountOfRoomLeft){
        String sql = String.format("update room set roomAvailability =roomAvailability-1 where roomID = '%s'", roomID);
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(RoomDA.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }