94 WCTME: Application Development and Case Study
//allow properties to override defaults
if (p != null) {
String standAloneString =
p.getProperty("itso.database.standalone","true");
standAlone = standAloneString.equalsIgnoreCase("true");
path = p.getProperty("path", "ITSODatabase");
driver = "jdbc:db2e:" + path + File.separator + "rentals";
}
//create directory if it doesn't exist
File dbDir = new File(path + File.separator + "rentals");
if (!dbDir.exists()) {
if (!dbDir.mkdirs()) {
System.out.println("Unable to create directory: " +
dbDir.getAbsolutePath());
}
}
return true;
}
/* (non-Javadoc)
* @see
com.itso.rentals.ITSORentalsDatabaseService#update2Arrived(java.lang.String)
*/
public boolean update2Arrived(String contractID) {
return updateStatus(contractID, Customer.ARRIVED);
}
/* (non-Javadoc)
* @see
com.itso.rentals.ITSORentalsDatabaseService#update2Exited(java.lang.String)
*/
public boolean update2Exited(String contractID) {
return updateStatus(contractID, Customer.EXITED);
}
/**
* Update this contract to the status provided
* @param contractID
* @param status
* @return
*/
private boolean updateStatus(String contractID, String status) {
boolean results = false;
//only allow one thread to access DB2e
synchronized (syncLock) {
Chapter 4. Database 95
try {
con = DriverManager.getConnection(driver);
st = con.createStatement();
st.executeUpdate("UPDATE CONTRACTS SET STATUS = '" + status + "'
WHERE CONTRACTID = '" + contractID + "'");
results = true;
} catch (SQLException sqlEx) {
System.out.println("SQL EXCEPTION: ERROR CODE: " +
sqlEx.getErrorCode() + " state: " + sqlEx.getSQLState());
sqlEx.printStackTrace();
} finally {
try {
st.close();
con.close();
} catch (SQLException e1) {
}
}
}
return results;
}
/* (non-Javadoc)
* @see
com.itso.rentals.ITSORentalsDatabaseService#getCustomer(java.lang.String)
*/
public Customer getCustomer(String contractID) {
Customer customer = null;
//only allow one thread to access DB2e
synchronized (syncLock) {
try {
con = DriverManager.getConnection(driver);
st = con.createStatement();
customer = new Customer();
//first find the contact and then...
rs = st.executeQuery("SELECT CONTRACTID, CUSTOMERID, FLIGHT,
STATUS, LOCATION FROM CONTRACTS WHERE CONTRACTID = '" + contractID + "'");
while (rs.next()) { // should only be 1
customer.setContractID(rs.getString(1));
customer.setCustomerID(rs.getString(2));
customer.setFlightInfo(rs.getString(3));
customer.setCurrentStatus(rs.getString(4));
customer.setVehicleLocation(rs.getString(5));
}
96 WCTME: Application Development and Case Study
rs.close();
// ...find the name that match the customer id
rs = st.executeQuery("SELECT FIRSTNAME, LASTNAME FROM CUSTOMERS
WHERE CUSTOMERID = '" + customer.getCustomerID() + "'");
while (rs.next()) { // should only be 1
customer.setFirstName(rs.getString(1));
customer.setLastName(rs.getString(2));
}
} 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();
con.close();
} catch (SQLException e1) {
}
}
}
return customer;
}
/* (non-Javadoc)
* @see com.itso.rentals.ITSORentalsDatabaseService#sync()
*/
public String sync(Properties userProps) {
if (standAlone) {
return "Data refreshed successfully";
} else {
return "Not implemented yet!";
}
}
/* (non-Javadoc)
* @see com.itso.rentals.ITSORentalsDatabaseService#getPending()
*/
public Vector getPendingCustomers() {
return getState(Customer.PENDING);
}
/* (non-Javadoc)
* @see com.itso.rentals.ITSORentalsDatabaseService#getArrived()
*/
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();
98 WCTME: Application Development and Case Study
con.close();
} catch (SQLException e1) {
}
}
}
return results;
}
/* (non-Javadoc)
* @see com.itso.rentals.ITSORentalsDatabaseService#insertPictures()
*/
public boolean insertPictures() {
//for demo (stand-alone) mode can put local pictures in the database
storeBlob("374906", "demo-images\\Image1.jpg");
storeBlob("GHJ453", "demo-images\\Image2.jpg");
storeBlob("JW3G56", "demo-images\\Image3.jpg");
storeBlob("DI4F56", "demo-images\\Image4.jpg");
storeBlob("XT345G", "demo-images\\Image5.jpg");
storeBlob("FT3H78", "demo-images\\Image6.jpg");
storeBlob("JK34S5", "demo-images\\Image7.jpg");
return true;
}
/* (non-Javadoc)
* @see com.itso.rentals.ITSORentalsDatabaseService#resetDemo()
*/
public boolean resetDemo() {
//reset the status field and clear the vechicle location field
boolean results = false;
//only allow one thread to access DB2e
synchronized (syncLock) {
try {
con = DriverManager.getConnection(driver);
st = con.createStatement();
st.executeUpdate("UPDATE CONTRACTS SET STATUS = 'pending'");
st.executeUpdate("UPDATE CONTRACTS SET LOCATION = '?'");
results = true;
} catch (SQLException sqlEx) {
System.out.println("SQL EXCEPTION: ERROR CODE: " +
sqlEx.getErrorCode() + " state: " + sqlEx.getSQLState());
sqlEx.printStackTrace();
} finally {
try {

Get IBM Workplace Client Technology Micro Edition Version 5.7.1: Application Development and Case Study now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.