1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2008-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package persist.gettingStarted; 10 11import java.io.File; 12 13import com.sleepycat.db.DatabaseException; 14import com.sleepycat.db.Environment; 15import com.sleepycat.db.EnvironmentConfig; 16 17import com.sleepycat.persist.EntityStore; 18import com.sleepycat.persist.StoreConfig; 19 20import java.io.FileNotFoundException; 21 22public class SimpleStoreGet { 23 24 private static File envHome = new File("./JEDB"); 25 26 private Environment envmnt; 27 private EntityStore store; 28 private SimpleDA sda; 29 30 // The setup() method opens the environment and store 31 // for us. 32 public void setup() 33 throws DatabaseException { 34 35 try { 36 EnvironmentConfig envConfig = new EnvironmentConfig(); 37 StoreConfig storeConfig = new StoreConfig(); 38 39 // Open the environment and entity store 40 envmnt = new Environment(envHome, envConfig); 41 store = new EntityStore(envmnt, "EntityStore", storeConfig); 42 } catch (FileNotFoundException fnfe) { 43 System.err.println("setup(): " + fnfe.toString()); 44 System.exit(-1); 45 } 46 } 47 48 public void shutdown() 49 throws DatabaseException { 50 51 store.close(); 52 envmnt.close(); 53 } 54 55 56 private void run() 57 throws DatabaseException { 58 59 setup(); 60 61 // Open the data accessor. This is used to store 62 // persistent objects. 63 sda = new SimpleDA(store); 64 65 // Instantiate and store some entity classes 66 SimpleEntityClass sec1 = sda.pIdx.get("keyone"); 67 SimpleEntityClass sec2 = sda.pIdx.get("keytwo"); 68 69 SimpleEntityClass sec4 = sda.sIdx.get("skeythree"); 70 71 System.out.println("sec1: " + sec1.getpKey()); 72 System.out.println("sec2: " + sec2.getpKey()); 73 System.out.println("sec4: " + sec4.getpKey()); 74 75 System.out.println("############ Doing pcursor ##########"); 76 for (SimpleEntityClass seci : sda.sec_pcursor ) { 77 System.out.println("sec from pcursor : " + seci.getpKey() ); 78 } 79 80 sda.pIdx.delete("keyone"); 81 System.out.println("############ Doing pcursor ##########"); 82 System.out.println("sec from pcursor : " + sda.sec_pcursor.first().getpKey()); 83 for (SimpleEntityClass seci : sda.sec_pcursor ) { 84 System.out.println("sec from pcursor : " + seci.getpKey() ); 85 } 86 87 System.out.println("############ Doing scursor ##########"); 88 for (SimpleEntityClass seci : sda.sec_scursor ) { 89 System.out.println("sec from scursor : " + seci.getpKey() ); 90 } 91 92 93 94 sda.close(); 95 shutdown(); 96 } 97 98 public static void main(String args[]) { 99 SimpleStoreGet ssg = new SimpleStoreGet(); 100 try { 101 ssg.run(); 102 } catch (DatabaseException dbe) { 103 System.err.println("SimpleStoreGet: " + dbe.toString()); 104 dbe.printStackTrace(); 105 } catch (Exception e) { 106 System.out.println("Exception: " + e.toString()); 107 e.printStackTrace(); 108 } 109 System.out.println("All done."); 110 } 111 112} 113