⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 airlinereservationbean.java

📁 BEA WebLogic Server 8.1大全 = BEA webLogic server 8.1 unleashed (美) Mark Artiges等著 袁毅 ... [等] 译 eng
💻 JAVA
字号:
/* 
 * WebLogic Server Unleashed
 * 
 */


package com.wlsunleashed.ejb.session.stateless;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


/**
 * This class Represents the Bean class for the  AirlineReservationBean EJB.
 * 
 * @version 1.0
 */
public class AirlineReservationBean implements SessionBean {
    /** Stores the initial context */
    private Context initialContext = null;

    /** Stores the data source */
    private DataSource dataSource = null;

    /** Stores the session context */
    private SessionContext context = null;

    /** The number of reserved seats */
    private int numReservedSeats = 5;

    /**
     * Constructor for AirlineReservationBean.
     */
    public AirlineReservationBean() {
        super();
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateless.
     *      AirlineReservationLocalObject#getFlightInfo(int)
     */
    public Properties getFlightInfo(int flightNumber)
                             throws AirlineReservationException {
        ResultSet rs = null;
        PreparedStatement queryFlightStmt = null;
        Connection connection = null;

        try {
            connection = dataSource.getConnection();

            String sql = "SELECT * FROM XYZAIRLINE.FLIGHTRECORD"
                         + " WHERE FLIGHT_NO = ? ";

            queryFlightStmt = connection.prepareStatement(sql);

            queryFlightStmt.setInt(1, flightNumber);
            rs = queryFlightStmt.executeQuery();

            if (!rs.next()) {
                return null;
            }

            Properties props = new Properties();
            props.put("FLIGHT_NO", rs.getString("FLIGHT_NO"));
            props.put("ORIGIN_AIRPORT", rs.getString("ORIGIN_AIRPORT"));
            props.put("DESTINATION_AIRPORT", 
                      rs.getString("DESTINATION_AIRPORT"));
            props.put("NUM_SEATS", rs.getString("NUM_SEATS"));
            props.put("TOTAL_COST_PER_SEAT", 
                      rs.getString("TOTAL_COST_PER_SEAT"));

            return props;
        } catch (Exception ex) {
            throw new AirlineReservationException(ex.getMessage());
        } finally {
            try {
                rs.close();
                queryFlightStmt.close();
                connection.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateless.
     *      AirlineReservationLocalObject#getFlightNumbers(String, String)
     */
    public int[] getFlightNumbers(String origin, String destination)
                           throws AirlineReservationException {
        ResultSet rs = null;
        Connection connection = null;
        PreparedStatement searchFlightStmt = null;

        try {
            connection = dataSource.getConnection();

            String sql = "SELECT FLIGHT_NO FROM XYZAIRLINE.FLIGHTRECORD"
                         + " WHERE UPPER(ORIGIN_AIRPORT) = UPPER(?)  AND "
                         + " UPPER(DESTINATION_AIRPORT) = UPPER(?) ";

            searchFlightStmt = connection.prepareStatement(sql);

            searchFlightStmt.setString(1, origin);
            searchFlightStmt.setString(2, destination);
            rs = searchFlightStmt.executeQuery();

            Vector v = new Vector();
            int count = 0;

            while (rs.next()) {
                Integer id = new Integer(rs.getInt("FLIGHT_NO"));
                v.addElement(id);
            }

            int[] ids = new int[v.size()];

            for (int i = 0; i < v.size(); i++) {
                ids[i] = ((Integer) v.get(i)).intValue();
            }

            return ids;
        } catch (Exception ex) {
            throw new AirlineReservationException(ex.getMessage());
        } finally {
            try {
                rs.close();
                searchFlightStmt.close();
                connection.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @see javax.ejb.SessionBean#setSessionContext( SessionContext)
     */
    public void setSessionContext(SessionContext aContext)
                           throws RemoteException {
        System.out.println(" setSessionContext called ");
        context = aContext;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        System.out.println("ejbActivate called");
    }

    /**
     * @see javax.ejb.SessionBean#ejbCreate
     */
    public void ejbCreate()
                   throws CreateException {
        System.out.println("AirlineReservationBean#ejbCreate called");

        // Create the prepared statements
        try {
            initialContext = new InitialContext();
            dataSource = (DataSource) initialContext.lookup(
                                 "java:comp/env/jdbc/AirlineData");
            numReservedSeats = ((Integer) initialContext.lookup(
                                        "java:comp/env/NumReservedSeats")).intValue();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new CreateException(ex.getMessage());
        }
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        System.out.println("ejbPassivate called");
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        System.out.println("ejbRemove called");

        try {
            initialContext.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @see com.wlsunleashed.ejb.session.stateless.
     *      AirlineReservationLocalObject#reserveSeats(int, int)
     */
    public void reserveSeats(int flightNumber, int numSeats)
                      throws AirlineReservationException {
        PreparedStatement updateFlightStmt = null;
        Connection connection = null;

        try {
            Properties props = getFlightInfo(flightNumber);
            int numSeatsInDB = Integer.parseInt((String) props.get("NUM_SEATS"));

            if ((numSeatsInDB < numReservedSeats)
                    || ((numSeatsInDB - numSeats) < numReservedSeats)) {
                throw new AirlineReservationException(
                        "Not enough seats. Reserved for platinum passengers");
            }

            connection = dataSource.getConnection();

            String sql = 
                "UPDATE XYZAIRLINE.FLIGHTRECORD SET NUM_SEATS = NUM_SEATS - ? "
                 + " WHERE FLIGHT_NO = ? ";

            updateFlightStmt = connection.prepareStatement(sql);
            updateFlightStmt.setInt(1, numSeats);
            updateFlightStmt.setInt(2, flightNumber);

            System.out.println("Updating.. " + updateFlightStmt.executeUpdate());
        } catch (Exception sqle) {
            sqle.printStackTrace();
            throw new AirlineReservationException(sqle.getMessage());
        } finally {
            try {
                updateFlightStmt.close();
                connection.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -