SHARE
TWEET

Untitled

a guest Dec 13th, 2016 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <title>Submitting Reservation</title>
  4.         <link rel="stylesheet" type="text/css" href="assets/home.css">
  5.     </head>
  6.     <body>
  7.     <%@ page import="java.sql.*" %>
  8.     <%@ page import="java.util.Date" %>
  9.     <%@ page import="java.text.SimpleDateFormat" %>
  10.     <%@include file="menu-bar.jsp" %>
  11. <%
  12.     //Set getConnection() parameters to connect to my database
  13.     String url = "jdbc:mysql://localhost:3306/internetairline";
  14.     String username = "root";
  15.     String DBpassword = "test$cozaser199";
  16.     Connection conn;
  17.     PreparedStatement pstmtFlightSet, pstmtAvail, pstmtReserve;
  18.     try {
  19.         Class.forName("com.mysql.jdbc.Driver");
  20.         conn = DriverManager.getConnection(url, username, DBpassword);
  21.  
  22.         //Find out if reservation was round flight or one way
  23.         boolean roundFlight;
  24.         String prepare;
  25.         if ( request.getParameter("roundFlight").equals("checked") )
  26.             roundFlight = true;
  27.         else
  28.             roundFlight = false;
  29.  
  30.         //Get flight info from sent parameters and set value to Strings
  31.         int dFlight = Integer.parseInt(request.getParameter("dFlight"));
  32.         int rFlight = Integer.parseInt(request.getParameter("rFlight"));
  33.  
  34.         // Depending on round flight or one way, build String to represent flightSet to be used in SELECT available query
  35.         String flightSet;
  36.         if (roundFlight)
  37.         {
  38.             prepare = "SELECT fid FROM flight f1 WHERE f1.orig =" + dFlight + " AND f1.dest ="+ rFlight +" UNION SELECT fid FROM flight f2 WHERE f2.orig ="+ rFlight +" AND f2.dest ="+ dFlight +";
  39.            pstmtFlightSet = conn.prepareStatement(prepare1);
  40.        }
  41.        else
  42.        {
  43.             prepare = "SELECT fid FROM flight f WHERE f.orig ="+ dFlight +" AND f.dest ="+ rFlight;
  44.            pstmtFlightSet = conn.prepareStatement(prepare);
  45.        }
  46.        pstmtFlightSet = conn.prepareStatement("SELECT available FROM flight WHERE fid IN (" + prepare + ")");
  47.        //Selecting available # of tickets to validate seat availability.
  48.        ResultSet rsAvail = pstmtFlightSet.executeQuery();
  49.        //Getting "available" field values and checking if negative.
  50.        if (roundFlight)
  51.        {
  52.            //Move through 2 records and save "available" value
  53.            rsAvail.next();
  54.            int dAvail = Integer.parseInt(rsAvail.getString(1)); //Get departure available column field, set to int to be compared
  55.            rsAvail.next();
  56.            int rAvail = Integer.parseInt(rsAvail.getString(1)); //Get return available field
  57.  
  58.            //If negative, flight(s) cannot be reserved. Redirect and alert to home page.
  59.            if (dAvail < 0 || rAvail < 0)
  60.            {
  61. %>              <script type="text/javascript">
  62.                    alert("No availability on this flight. Please decrease the number of tickets you are reserving.")
  63.                    window.location = 'home.jsp'
  64.                </script>
  65. <%          }
  66.        }
  67.        else    //one way flight
  68.        {
  69.            //Move to first and only record and save available value.
  70.            rsAvail.next();
  71.            int dAvail = Integer.parseInt(rsAvail.getString(1)); //Get departure available column field, set to int to be compared
  72.  
  73.            //If negative, flight(s) cannot be reserved. Redirect and alert to home page.
  74.            if (dAvail < 0)
  75.            {
  76. %>              <script type="text/javascript">
  77.                    alert("No availability on this flight. Please decrease the number of tickets you are reserving.")
  78.                    window.location = 'home.jsp'
  79.                    </script>
  80. <%          }
  81.        }
  82.        //Set current date to be added in reservation insert
  83.        Date dateCurrent = new Date();
  84.        SimpleDateFormat fdate = new SimpleDateFormat("yyyy-MM-dd");
  85.        String date = fdate.format(dateCurrent);
  86.  
  87.        //Getting all info necessary for inserting a flight reservation into table
  88.        //rfilight only needed for round flight, so we must check if round flight
  89.        if (roundFlight)
  90.        {
  91.            pstmtReserve = conn.prepareStatement("INSERT INTO reservation(cid, dfid, rfid, qty, cardnum, cardmonth, cardyear, order_date) VALUES (?,?,?,?,?,?,?,?)");
  92.            pstmtReserve.clearParameters();
  93.            pstmtReserve.setInt(1, Integer.parseInt(request.getParameter("cid")));
  94.            pstmtReserve.setInt(2, dFlight);
  95.            pstmtReserve.setInt(3, rFlight);
  96.            pstmtReserve.setInt(4, Integer.parseInt(request.getParameter("numTickets")));
  97.            pstmtReserve.setInt(5, Integer.parseInt(request.getParameter("cardNumber")));
  98.            pstmtReserve.setInt(6, Integer.parseInt(request.getParameter("cardMonth")));
  99.            pstmtReserve.setInt(7, Integer.parseInt(request.getParameter("cardYear")));
  100.            pstmtReserve.setString(8, date);
  101.        }
  102.        //One way
  103.        else
  104.        {
  105.            pstmtReserve = conn.prepareStatement("INSERT INTO reservation(cid, dfid, qty, cardnum, cardmonth, cardyear, order_date) VALUES (?,?,?,?,?,?,?)");
  106.            pstmtReserve.clearParameters();
  107.            pstmtReserve.setInt(1, Integer.parseInt(request.getParameter("cid")));
  108.            pstmtReserve.setInt(2, dFlight);
  109.            pstmtReserve.setInt(3, Integer.parseInt(request.getParameter("numTickets")));
  110.            pstmtReserve.setInt(4, Integer.parseInt(request.getParameter("cardNumber")));
  111.            pstmtReserve.setInt(5, Integer.parseInt(request.getParameter("cardMonth")));
  112.            pstmtReserve.setInt(6, Integer.parseInt(request.getParameter("cardYear")));
  113.            pstmtReserve.setString(7, date);
  114.        }
  115.        //Insert into reservation based on round or one way flight
  116.        int numRows= pstmtReserve.executeUpdate();
  117.  
  118. %>      <script type="text/javascript">
  119.            alert("Your flight has been reserved!")
  120.            window.location = 'home.jsp'
  121.        </script>
  122. <%
  123.    rsAvail.close();
  124.    pstmtFlightSet.close();
  125.    pstmtReserve.close();
  126.    conn.close();
  127.    }//end try, start catch block for any errors/exceptions
  128.    catch(SQLException ex) {
  129.        out.println("SQLException: " + ex.getMessage());
  130.        out.println("SQLState: " + ex.getSQLState());
  131.        out.println("VendorError: " + ex.getErrorCode());
  132.        ex.printStackTrace();
  133.    }
  134. %>
  135.    </body>
  136. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top