Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

ExampleDatabaseRead.java

00001 // File: ExampleDatabaseRead
00002 
00003 package db.GettingStarted;
00004 
00005 import com.sleepycat.bind.EntryBinding;
00006 import com.sleepycat.bind.serial.SerialBinding;
00007 import com.sleepycat.bind.tuple.TupleBinding;
00008 import com.sleepycat.db.Cursor;
00009 import com.sleepycat.db.DatabaseEntry;
00010 import com.sleepycat.db.DatabaseException;
00011 import com.sleepycat.db.LockMode;
00012 import com.sleepycat.db.OperationStatus;
00013 import com.sleepycat.db.SecondaryCursor;
00014 
00015 import java.io.IOException;
00016 
00017 public class ExampleDatabaseRead {
00018 
00019     private static String myDbsPath = "./";
00020 
00021     // Encapsulates the database environment and databases.
00022     private static MyDbs myDbs = new MyDbs();
00023 
00024     private static TupleBinding inventoryBinding;
00025     private static EntryBinding vendorBinding;
00026 
00027     // The item to locate if the -s switch is used
00028     private static String locateItem;
00029 
00030     private static void usage() {
00031         System.out.println("ExampleDatabaseRead [-h <env directory>]" +
00032                            "[-s <item to locate>]");
00033         System.exit(-1);
00034     }
00035 
00036     public static void main(String args[]) {
00037         ExampleDatabaseRead edr = new ExampleDatabaseRead();
00038         try {
00039             edr.run(args);
00040         } catch (DatabaseException dbe) {
00041             System.err.println("ExampleDatabaseRead: " + dbe.toString());
00042             dbe.printStackTrace();
00043         } finally {
00044             myDbs.close();
00045         }
00046         System.out.println("All done.");
00047     }
00048 
00049     private void run(String args[])
00050         throws DatabaseException {
00051         // Parse the arguments list
00052         parseArgs(args);
00053 
00054         myDbs.setup(myDbsPath);
00055 
00056         // Setup our bindings.
00057         inventoryBinding = new InventoryBinding();
00058         vendorBinding =
00059              new SerialBinding(myDbs.getClassCatalog(),
00060                                Vendor.class);
00061 
00062         if (locateItem != null) {
00063             showItem();
00064         } else {
00065             showAllInventory();
00066         }
00067     }
00068 
00069     private void showItem() throws DatabaseException {
00070 
00071         SecondaryCursor secCursor = null;
00072         try {
00073             // searchKey is the key that we want to find in the
00074             // secondary db.
00075             DatabaseEntry searchKey =
00076                 new DatabaseEntry(locateItem.getBytes("UTF-8"));
00077 
00078             // foundKey and foundData are populated from the primary
00079             // entry that is associated with the secondary db key.
00080             DatabaseEntry foundKey = new DatabaseEntry();
00081             DatabaseEntry foundData = new DatabaseEntry();
00082 
00083             // open a secondary cursor
00084             secCursor =
00085                 myDbs.getNameIndexDB().openSecondaryCursor(null, null);
00086 
00087             // Search for the secondary database entry.
00088             OperationStatus retVal =
00089                 secCursor.getSearchKey(searchKey, foundKey,
00090                     foundData, LockMode.DEFAULT);
00091 
00092             // Display the entry, if one is found. Repeat until no more
00093             // secondary duplicate entries are found
00094             while(retVal == OperationStatus.SUCCESS) {
00095                 Inventory theInventory =
00096                     (Inventory)inventoryBinding.entryToObject(foundData);
00097                 displayInventoryRecord(foundKey, theInventory);
00098                 retVal = secCursor.getNextDup(searchKey, foundKey,
00099                     foundData, LockMode.DEFAULT);
00100             }
00101         } catch (Exception e) {
00102             System.err.println("Error on inventory secondary cursor:");
00103             System.err.println(e.toString());
00104             e.printStackTrace();
00105         } finally {
00106             if (secCursor != null) {
00107                 secCursor.close();
00108             }
00109         }
00110     }
00111 
00112     private void showAllInventory()
00113         throws DatabaseException {
00114         // Get a cursor
00115         Cursor cursor = myDbs.getInventoryDB().openCursor(null, null);
00116 
00117         // DatabaseEntry objects used for reading records
00118         DatabaseEntry foundKey = new DatabaseEntry();
00119         DatabaseEntry foundData = new DatabaseEntry();
00120 
00121         try { // always want to make sure the cursor gets closed
00122             while (cursor.getNext(foundKey, foundData,
00123                         LockMode.DEFAULT) == OperationStatus.SUCCESS) {
00124                 Inventory theInventory =
00125                     (Inventory)inventoryBinding.entryToObject(foundData);
00126                 displayInventoryRecord(foundKey, theInventory);
00127             }
00128         } catch (Exception e) {
00129             System.err.println("Error on inventory cursor:");
00130             System.err.println(e.toString());
00131             e.printStackTrace();
00132         } finally {
00133             cursor.close();
00134         }
00135     }
00136 
00137     private void displayInventoryRecord(DatabaseEntry theKey,
00138                                         Inventory theInventory)
00139         throws DatabaseException {
00140 
00141         String theSKU = new String(theKey.getData());
00142         System.out.println(theSKU + ":");
00143         System.out.println("\t " + theInventory.getItemName());
00144         System.out.println("\t " + theInventory.getCategory());
00145         System.out.println("\t " + theInventory.getVendor());
00146         System.out.println("\t\tNumber in stock: " +
00147             theInventory.getVendorInventory());
00148         System.out.println("\t\tPrice per unit:  " +
00149             theInventory.getVendorPrice());
00150         System.out.println("\t\tContact: ");
00151 
00152         DatabaseEntry searchKey = null;
00153         try {
00154             searchKey =
00155                 new DatabaseEntry(theInventory.getVendor().getBytes("UTF-8"));
00156         } catch (IOException willNeverOccur) {}
00157         DatabaseEntry foundVendor = new DatabaseEntry();
00158 
00159         if (myDbs.getVendorDB().get(null, searchKey, foundVendor,
00160                 LockMode.DEFAULT) != OperationStatus.SUCCESS) {
00161             System.out.println("Could not find vendor: " +
00162                 theInventory.getVendor() + ".");
00163             System.exit(-1);
00164         } else {
00165             Vendor theVendor =
00166                 (Vendor)vendorBinding.entryToObject(foundVendor);
00167             System.out.println("\t\t " + theVendor.getAddress());
00168             System.out.println("\t\t " + theVendor.getCity() + ", " +
00169                 theVendor.getState() + " " + theVendor.getZipcode());
00170             System.out.println("\t\t Business Phone: " +
00171                 theVendor.getBusinessPhoneNumber());
00172             System.out.println("\t\t Sales Rep: " +
00173                                 theVendor.getRepName());
00174             System.out.println("\t\t            " +
00175                 theVendor.getRepPhoneNumber());
00176        }
00177     }
00178 
00179     protected ExampleDatabaseRead() {}
00180 
00181     private static void parseArgs(String args[]) {
00182         int nArgs = args.length;
00183         for(int i = 0; i < args.length; ++i) {
00184             if (args[i].startsWith("-")) {
00185                 switch(args[i].charAt(1)) {
00186                     case 'h':
00187                         if (i < nArgs) {
00188                             myDbsPath = new String(args[++i]);
00189                         }
00190                     break;
00191                     case 's':
00192                         if (i < nArgs) {
00193                             locateItem = new String(args[++i]);
00194                         }
00195                     break;
00196                     default:
00197                         usage();
00198                 }
00199             }
00200         }
00201     }
00202 }

Generated on Sun Dec 25 12:14:27 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2