1package persist.gettingStarted; 2 3import java.io.File; 4 5import com.sleepycat.db.DatabaseException; 6import com.sleepycat.db.Environment; 7import com.sleepycat.db.EnvironmentConfig; 8 9import com.sleepycat.persist.EntityStore; 10import com.sleepycat.persist.StoreConfig; 11 12import java.io.FileNotFoundException; 13 14public class SimpleStorePut { 15 16 private static File envHome = new File("./JEDB"); 17 18 private Environment envmnt; 19 private EntityStore store; 20 private SimpleDA sda; 21 22 // The setup() method opens the environment and store 23 // for us. 24 public void setup() 25 throws DatabaseException { 26 27 EnvironmentConfig envConfig = new EnvironmentConfig(); 28 StoreConfig storeConfig = new StoreConfig(); 29 30 envConfig.setAllowCreate(true); 31 storeConfig.setAllowCreate(true); 32 33 try { 34 // Open the environment and entity store 35 envmnt = new Environment(envHome, envConfig); 36 store = new EntityStore(envmnt, "EntityStore", storeConfig); 37 } catch (FileNotFoundException fnfe) { 38 System.err.println("setup(): " + fnfe.toString()); 39 System.exit(-1); 40 } 41 } 42 43 // Close our environment and store. 44 public void shutdown() 45 throws DatabaseException { 46 47 store.close(); 48 envmnt.close(); 49 } 50 51 52 private void run() 53 throws DatabaseException { 54 55 setup(); 56 57 // Open the data accessor. This is used to store 58 // persistent objects. 59 sda = new SimpleDA(store); 60 61 // Instantiate and store some entity classes 62 SimpleEntityClass sec1 = new SimpleEntityClass(); 63 SimpleEntityClass sec2 = new SimpleEntityClass(); 64 SimpleEntityClass sec3 = new SimpleEntityClass(); 65 SimpleEntityClass sec4 = new SimpleEntityClass(); 66 SimpleEntityClass sec5 = new SimpleEntityClass(); 67 68 sec1.setpKey("keyone"); 69 sec1.setsKey("skeyone"); 70 71 sec2.setpKey("keytwo"); 72 sec2.setsKey("skeyone"); 73 74 sec3.setpKey("keythree"); 75 sec3.setsKey("skeytwo"); 76 77 sec4.setpKey("keyfour"); 78 sec4.setsKey("skeythree"); 79 80 sec5.setpKey("keyfive"); 81 sec5.setsKey("skeyfour"); 82 83 sda.pIdx.put(sec1); 84 sda.pIdx.put(sec2); 85 sda.pIdx.put(sec3); 86 sda.pIdx.put(sec4); 87 sda.pIdx.put(sec5); 88 89 sda.close(); 90 91 shutdown(); 92 } 93 94 public static void main(String args[]) { 95 SimpleStorePut ssp = new SimpleStorePut(); 96 try { 97 ssp.run(); 98 } catch (DatabaseException dbe) { 99 System.err.println("SimpleStorePut: " + dbe.toString()); 100 dbe.printStackTrace(); 101 } catch (Exception e) { 102 System.out.println("Exception: " + e.toString()); 103 e.printStackTrace(); 104 } 105 System.out.println("All done."); 106 } 107 108} 109