Chapter 4. Database 97
public Vector getArrivedCustomers() {
return getState(Customer.ARRIVED);
}
/**
* Returns a Vector of Customers that have the requested state
* @param status
* @return
*/
private Vector getState(String status) {
Vector results = new Vector();
//only allow one thread to access DB2e
synchronized (syncLock) {
try {
con = DriverManager.getConnection(driver);
st = con.createStatement();
//join the two tables and find the desired results
rs = st.executeQuery("SELECT CUSTOMERS.CUSTOMERID,
CONTRACTS.CONTRACTID, CUSTOMERS.FIRSTNAME, CUSTOMERS.LASTNAME,
CONTRACTS.FLIGHT, CONTRACTS.STATUS, CONTRACTS.LOCATION FROM CUSTOMERS,
CONTRACTS WHERE CUSTOMERS.CUSTOMERID = CONTRACTS.CUSTOMERID AND
CONTRACTS.STATUS = '"+ status +"' ORDER BY CUSTOMERS.LASTNAME,
CUSTOMERS.FIRSTNAME");
while (rs.next()) {
Customer customer = new Customer();
customer.setCustomerID(rs.getString(1));
customer.setContractID(rs.getString(2));
customer.setFirstName(rs.getString(3));
customer.setLastName(rs.getString(4));
customer.setFlightInfo(rs.getString(5));
customer.setCurrentStatus(rs.getString(6));
customer.setVehicleLocation(rs.getString(7));
//add to results Vector
results.add(customer);
}
} catch (SQLException sqlEx) {
System.out.println("SQL EXCEPTION: ERROR CODE: " +
sqlEx.getErrorCode() + " state: " + sqlEx.getSQLState());
sqlEx.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
st.close();