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 SimpleStorePut { 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 EnvironmentConfig envConfig = new EnvironmentConfig(); 36 StoreConfig storeConfig = new StoreConfig(); 37 38 envConfig.setAllowCreate(true); 39 storeConfig.setAllowCreate(true); 40 41 try { 42 // Open the environment and entity store 43 envmnt = new Environment(envHome, envConfig); 44 store = new EntityStore(envmnt, "EntityStore", storeConfig); 45 } catch (FileNotFoundException fnfe) { 46 System.err.println("setup(): " + fnfe.toString()); 47 System.exit(-1); 48 } 49 } 50 51 // Close our environment and store. 52 public void shutdown() 53 throws DatabaseException { 54 55 store.close(); 56 envmnt.close(); 57 } 58 59 60 private void run() 61 throws DatabaseException { 62 63 setup(); 64 65 // Open the data accessor. This is used to store 66 // persistent objects. 67 sda = new SimpleDA(store); 68 69 // Instantiate and store some entity classes 70 SimpleEntityClass sec1 = new SimpleEntityClass(); 71 SimpleEntityClass sec2 = new SimpleEntityClass(); 72 SimpleEntityClass sec3 = new SimpleEntityClass(); 73 SimpleEntityClass sec4 = new SimpleEntityClass(); 74 SimpleEntityClass sec5 = new SimpleEntityClass(); 75 76 sec1.setpKey("keyone"); 77 sec1.setsKey("skeyone"); 78 79 sec2.setpKey("keytwo"); 80 sec2.setsKey("skeyone"); 81 82 sec3.setpKey("keythree"); 83 sec3.setsKey("skeytwo"); 84 85 sec4.setpKey("keyfour"); 86 sec4.setsKey("skeythree"); 87 88 sec5.setpKey("keyfive"); 89 sec5.setsKey("skeyfour"); 90 91 sda.pIdx.put(sec1); 92 sda.pIdx.put(sec2); 93 sda.pIdx.put(sec3); 94 sda.pIdx.put(sec4); 95 sda.pIdx.put(sec5); 96 97 sda.close(); 98 99 shutdown(); 100 } 101 102 public static void main(String args[]) { 103 SimpleStorePut ssp = new SimpleStorePut(); 104 try { 105 ssp.run(); 106 } catch (DatabaseException dbe) { 107 System.err.println("SimpleStorePut: " + dbe.toString()); 108 dbe.printStackTrace(); 109 } catch (Exception e) { 110 System.out.println("Exception: " + e.toString()); 111 e.printStackTrace(); 112 } 113 System.out.println("All done."); 114 } 115 116} 117