0

I have a table called Rooms and this how the table looks like

 CREATE TABLE Rooms 
 (
     id INT AUTO_INCREMENT,
     RoomNumber VARCHAR(32) NOT NULL,
     AvailableSeats INT NOT NULL,
     PRIMARY KEY (id)   
 ) 

In my code below, i am able to fill the combo box with the room numbers in the database. But what i want to do is, when i select a room number, the available seats for that room number must update.

For instance, when i select

RoomNumber 4, available seats 4

RoomNumber 3, available seats 2

How can i achieve this ?

Query

// query to fetch room numbers to combo box

public void FindRoom() {
    String query = "Select * from Rooms";
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while(rs.next()) {
            String getRoomNumber = rs.getString("RoomNumber");
            FindRoom.addItem(getRoomNumber);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

//query to get respective available seats for every room number

public void FindSeats() {
    String query = "Select * from Rooms where RoomNumber = "+FindRoom.getSelectedItem();
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while(rs.next()) {
            String getAvailableSeat = rs.getString("AvailableSeats");
            seats.setText(getAvailableSeat);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • 2
    Does your code work? What's the problem? – shmosel Aug 20 '17 at 08:42
  • since the number of available rooms is in the same table your first query already returns that value. Therefor you could handle that in the same method. – Timothy Truckle Aug 20 '17 at 08:43
  • Maybe you need trigger in DB for updates number of available seats? – ema Aug 20 '17 at 08:45
  • @shmosel it works but i am not able to handle the respective available seats for every room number –  Aug 20 '17 at 08:46
  • If you use `Swing`, you could put all data from the table into a collection, and make a specialized `ComboBoxModel` based on that collection. After the user selects an item, you coult get all the fields from the collection's item... – Usagi Miyamoto Aug 20 '17 at 08:46
  • 2
    Please take the [tour](/tour), have a look around, and read through the [help center](/help) , in particular [How do I ask a good question?](/help/how-to-ask). Also read (and follow) the [Java Naming Conventions](/www.oracle.com/technetwork/java/codeconventions-135099.html) and [What is so bad about singletons?](https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons). – Timothy Truckle Aug 20 '17 at 08:54
  • @XamarinLearner thanks! – Timothy Truckle Aug 20 '17 at 08:55

0 Answers0