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);
}
}