5577l6 Listing 6. UseBook.java package il.co.lerner.book; /* Import our entity bean */ import il.co.lerner.book.Book; import il.co.lerner.book.BookHome; /* Import the RMI and JDNI items we need */ import javax.rmi.PortableRemoteObject; import javax.naming.InitialContext; import java.util.Properties; import java.util.Collection; import java.util.Iterator; import java.io.FileInputStream; /* This application demonstrates how we can INSERT, UPDATE, and SELECT information from a database table using EJB's objects instead of SQL. */ public class UseBook { public static void main(String[] args) { final int testPrimaryKey = 11; try { System.out.println("(UseBook) About to get an InitialContext"); // Get a naming context InitialContext jndiContext = new InitialContext(); System.out.println("(UseBook) About to invoke jndiContext.lookup()"); // Get a reference to a Book Bean Object ref = jndiContext.lookup("Book"); System.out.println("(UseBook) About to use the reference from JNDI"); // Get a reference from this to the Bean's Home interface BookHome home = (BookHome) PortableRemoteObject.narrow (ref, BookHome.class); System.out.println("(UseBook) About to create a new book"); Book book = home.create(testPrimaryKey, "Book title", "AuthorFirst AuthorLast", "PublisherName", 10.50); System.out.println("(UseBook) Successfully added!"); String title = book.getTitle(); String author = book.getAuthor(); System.out.println("(UseBook) The title is '"+ title + "' and the author is '" + author + "'"); // Now let's change the information book.setTitle("New title"); book.setAuthor("New author"); // Let's retrieve and double-check the information String checkTitle = book.getTitle(); String checkAuthor = book.getAuthor(); System.out.println("(UseBook) After changes, the title is '"+ checkTitle + "' and the author is '" + checkAuthor + "'"); // Now let's get all of the books Collection bookCollection = home.findAll(); Iterator bookIterator = bookCollection.iterator(); while (bookIterator.hasNext()) { Book aBook = (Book) bookIterator.next(); System.out.println("(UseBook) Retrieved book has id '"+ aBook.getId() + "', title '"+ aBook.getTitle() + "' and an author of '" + aBook.getAuthor() + "'"); } } catch(Exception e) { System.out.println(e.toString()); } } }