预订时更新房间:酒店预订系统,Java,MySQL

时间:2016-05-01 04:33:04

标签: java mysql eclipse jdbc mysql-workbench

我正在用java和mysql做一个酒店预订系统。我的预订页面包含客户的输入详细信息,登记入住和退房。现在,插入客户详细信息,预订详细信息,付款详细信息正常,但我无法更新特定客户预订的房间。

这是我的java代码:

String dml4 = "select * from room where roomType=? and statusAvailable='Yes'";
                            PreparedStatement ps4 = con.prepareStatement(dml4);
                            ps4.setString(1, cbfirst.getSelectedItem().toString());
                            ResultSet rs = ps4.executeQuery();

                            if (!rs.next() ) {    
                                 JOptionPane.showMessageDialog(null, "No available rooms with roomType specified", "WARNING!", JOptionPane.ERROR_MESSAGE);
                                } 
                            else{
                                //setting room in room table to be occupied
                                String dml5 = "update room set statusAvailable='No' where roomType=?";
                                PreparedStatement ps5 = con.prepareStatement(dml5);
                                ps5.setString(1, cbfirst.getSelectedItem().toString());
                                ps5.executeUpdate();

                            }

问题是,作为roomType='?',所有具有特定类型的房间(例如,“单身”)都被设置为占用。

如何只预订该特定类型的一个房间?

//inserting into guest table
                            String dml = "Insert into guest (fname,lname, dateOfBirth, gender, phone) values (?,?,?,?,?)";
                            PreparedStatement ps = con.prepareStatement(dml, Statement.RETURN_GENERATED_KEYS);
                            ps.setString(1, fname.getText()); 
                            ps.setString(2, lname.getText());
                            ps.setString(3, ((JFormattedTextField)dob.getDateEditor().getUiComponent()).getText());
                            ps.setString(4, cbGender.getSelectedItem().toString());
                            ps.setString(5, formaPhone.getText());
                            ps.executeUpdate();
                            JOptionPane.showMessageDialog(null, "New Guest Added Successfully");

                            ResultSet keyOfGuest = ps.getGeneratedKeys();    
                            keyOfGuest.next();  
                            int keyGuest = keyOfGuest.getInt(1);
                            String keyGuest2 = String.valueOf(keyGuest);




                            //inserting into booking table
                            String dml2 = "insert into booking(guestID, checkIn, checkOut, totalPaymentDueAmt) values (?, ?, ?, ?)";
                            PreparedStatement ps2 = con.prepareStatement(dml2, Statement.RETURN_GENERATED_KEYS);
                            ps2.setString(1, keyGuest2);
                            ps2.setString(2, ((JFormattedTextField)checkin.getDateEditor().getUiComponent()).getText());
                            ps2.setString(3, ((JFormattedTextField)checkout.getDateEditor().getUiComponent()).getText());
                            ps2.setString(4, tfTotal.getText());
                            ps2.executeUpdate();

                            //inserting into payment table
                            ResultSet keyOfBook = ps2.getGeneratedKeys();    
                            keyOfBook.next();  
                            int keyBook = keyOfBook.getInt(1);
                            String keyBook2 = String.valueOf(keyBook);
                            String dml3 = "insert into payment(bookingID, guestID, paymentMethod, curPaymentAmt, additionalPayment) values (?,?,?,?,?)";
                            PreparedStatement ps3 = con.prepareStatement(dml3);
                            ps3.setString(1, keyBook2);
                            ps3.setString(2, keyGuest2);
                            ps3.setString(3, cbpay.getSelectedItem().toString());
                            ps3.setString(4, tfTotal.getText());
                            ps3.setString(5, null);
                            ps3.executeUpdate();


                            //String dml4 = "update room set statusAvailable='No' where roomType="+cbfirst.getSelectedItem().toString();
                            String dml4 = "select * from room where roomType=? and statusAvailable='Yes'";
                            PreparedStatement ps4 = con.prepareStatement(dml4);
                            ps4.setString(1, cbfirst.getSelectedItem().toString());
                            ResultSet rs = ps4.executeQuery();

                            if (!rs.next() ) {    
                                 JOptionPane.showMessageDialog(null, "No available rooms with roomType specified", "WARNING!", JOptionPane.ERROR_MESSAGE);
                                } 
                            else{
                                //setting room in room table to be occupied
                                String dml5 = "update room set statusAvailable='No' where roomType=? limit 1";
                                PreparedStatement ps5 = con.prepareStatement(dml5);
                                ps5.setString(1, cbfirst.getSelectedItem().toString());
                                ps5.executeUpdate();

                            }                                 

编辑:这是我的完整代码,JButton已执行其操作。

0 个答案:

没有答案