private void CancelTicketButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelTicketButtonActionPerformed

        
        int selRow = ViewHistoryTable.getSelectedRow();

        if (selRow == -1) {
            jLabel1.setText("Please select a ticket to continue");
            return;
        }
        
        jLabel1.setText("Ticket History"); 
        String message = "Are you sure you want to delete? ";
        int reply = javax.swing.JOptionPane.showConfirmDialog(this, message, "Warning", javax.swing.JOptionPane.YES_NO_OPTION);
        if (reply == javax.swing.JOptionPane.YES_OPTION) {

            String ticketno = (String) ViewHistoryTable.getModel().getValueAt(selRow, 0);
            try {
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "oracle");
                PreparedStatement ps = con.prepareStatement("select dfid,afid,ddate,adate,pid,seatingclass "
                        + "from ticket where tid=?");
                ps.setString(1, ticketno);
                ResultSet rs = ps.executeQuery();
                    

                if (rs.next()) {
                    String fromTimeStamp = rs.getString(3).substring(0, 19);
                    String toTimeStamp = rs.getString(4).substring(0, 19);

                    Statement s = con.createStatement();
                    ResultSet r1 = s.executeQuery("select sysdate from dual");
                    r1.next();

                    if (rs.getDate(3).compareTo(r1.getDate(1)) < 0) {
                        javax.swing.JOptionPane.showMessageDialog(this, "Cannot cancel ticket. Travel date is over!");
                        //  con.close();
                        // return;
                    }

                    s = con.createStatement();
                    r1 = s.executeQuery("select flightno from flight where fid=" + rs.getString(1));
                    r1.next();
                    String FlightNo = r1.getString(1);
                    
                    s = con.createStatement();
                    if (rs.getInt(6) == 0) {
                        s.executeUpdate("update particularflight set availeco=availeco+1 "
                                + "where fid IN (select fid from flight where Flightno='" + FlightNo + "')"
                                + "and to_char(depdate,'yyyy-mm-dd hh24:mi:ss')>='" + fromTimeStamp + "' "
                                + "and to_char(arrdate,'yyyy-mm-dd hh24:mi:ss')<='" + toTimeStamp + "'");
                    } else if (rs.getInt(6) == 1) {
                        s.executeUpdate("update particularflight set availbus=availbus+1 "
                                + "where fid IN (select fid from flight where Flightno='" + FlightNo + "')"
                                + "and to_char(depdate,'yyyy-mm-dd hh24:mi:ss')>='" + fromTimeStamp + "' "
                                + "and to_char(arrdate,'yyyy-mm-dd hh24:mi:ss')<='" + toTimeStamp + "'");
                    }

                }

                Statement s = con.createStatement();

                s.executeUpdate("delete from userticket where tid=" + ticketno);
                s.executeUpdate("delete from ticket where tid=" + ticketno);
                s.executeUpdate("delete from passenger where pid=" + rs.getInt(5));

                javax.swing.JOptionPane.showMessageDialog(this, "Deleted");
                con.close();
                TicketHistoryButtonActionPerformed(evt);
            } catch (SQLException se) {
                System.out.println(se);
            }

        } 


    }//GEN-LAST:event_CancelTicketButtonActionPerformed