import java.util.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class DvdClient { public static void main(String[] args) { try { Context initialContext = new InitialContext(); Object initialContextObject = initialContext.lookup("java:comp/env/ejb/SimpleDvd"); DvdHome dvdHome = (DvdHome)PortableRemoteObject.narrow(initialContextObject, DvdHome.class); Dvd bis; Collection dvdCheck; Iterator dvdCheckIterator; //Check to see if this pk already exists. //Maybe not an efficent way to do things, // but an interesting example. dvdCheck = dvdHome.findByIsbn("078063344X"); if (dvdCheck.isEmpty()) { bis = dvdHome.create("078063344X", "Best of Show", "WS "); } else { dvdCheckIterator = dvdCheck.iterator(); bis = (Dvd)dvdCheckIterator.next(); } System.out.println("after create: " + bis.getAsString()); bis.setTitle("Best in Show"); System.out.println("after change title: " + bis.getAsString()); Collection collection = dvdHome.findByTitle("Best in Show"); Iterator iterator=collection.iterator(); while (iterator.hasNext()) { Dvd dvd = (Dvd)iterator.next(); System.out.println("in bis loop: " + dvd.getAsString()); } Dvd rlr = null; dvdCheck = dvdHome.findByIsbn("076783738X"); if (dvdCheck.isEmpty()) { rlr = dvdHome.create("076783738X", "Run Lola Run", "WS-FS"); } Dvd pf = null; dvdCheck = dvdHome.findByIsbn("0780634551"); if (dvdCheck.isEmpty()) { pf = dvdHome.create("0780634551", "Pink Flamingos", "WS "); } Dvd sil = null; dvdCheck = dvdHome.findByIsbn("0788818937"); if (dvdCheck.isEmpty()) { sil = dvdHome.create("0788818937", "Shakespeare in Love", "WS "); } Dvd tpb = null; dvdCheck = dvdHome.findByIsbn("0792850769"); if (dvdCheck.isEmpty()) { tpb = dvdHome.create("0792850769", "The Princess Bride", "WS "); } collection = dvdHome.findByTitleWith("in"); iterator=collection.iterator(); while (iterator.hasNext()) { Dvd dvd = (Dvd)iterator.next(); System.out.println("in like loop: " + dvd.getAsString()); } if (bis != null) {bis.remove();} //note: will remove from table if (rlr != null) {rlr.remove();} //note: will remove from table only if we created rlr // in this run if (pf != null) {pf.remove();} //note: will remove from table only if we created pf // in this run if (sil != null) {sil.remove();} //note: will remove from table only if we created sil // in this run if (tpb != null) {tpb.remove();} //note: will remove from table only if we created tpb // in this run } catch (Exception exception) { System.err.println("Caught an exception." ); exception.printStackTrace(); } } }