1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1997,2008 Oracle. All rights reserved. 5 * 6 * $Id: AccessExample.java,v 12.8 2008/01/08 20:58:32 bostic Exp $ 7 */ 8 9 10package db; 11 12import com.sleepycat.db.*; 13import java.io.File; 14import java.io.FileNotFoundException; 15import java.io.InputStreamReader; 16import java.io.IOException; 17import java.io.PrintStream; 18 19class AccessExample { 20 private static final int EXIT_SUCCESS = 0; 21 private static final int EXIT_FAILURE = 1; 22 23 public AccessExample() { 24 } 25 26 public static void usage() { 27 System.out.println("usage: java " + 28 "db.AccessExample [-r] [database]\n"); 29 System.exit(EXIT_FAILURE); 30 } 31 32 public static void main(String[] argv) { 33 boolean removeExistingDatabase = false; 34 String databaseName = "access.db"; 35 36 for (int i = 0; i < argv.length; i++) { 37 if (argv[i].equals("-r")) 38 removeExistingDatabase = true; 39 else if (argv[i].equals("-?")) 40 usage(); 41 else if (argv[i].startsWith("-")) 42 usage(); 43 else { 44 if ((argv.length - i) != 1) 45 usage(); 46 databaseName = argv[i]; 47 break; 48 } 49 } 50 51 try { 52 AccessExample app = new AccessExample(); 53 app.run(removeExistingDatabase, databaseName); 54 } catch (DatabaseException dbe) { 55 System.err.println("AccessExample: " + dbe.toString()); 56 System.exit(EXIT_FAILURE); 57 } catch (FileNotFoundException fnfe) { 58 System.err.println("AccessExample: " + fnfe.toString()); 59 System.exit(EXIT_FAILURE); 60 } 61 System.exit(EXIT_SUCCESS); 62 } 63 64 // Prompts for a line, and keeps prompting until a non blank 65 // line is returned. Returns null on error. 66 // 67 public static String askForLine(InputStreamReader reader, 68 PrintStream out, String prompt) { 69 String result = ""; 70 while (result != null && result.length() == 0) { 71 out.print(prompt); 72 out.flush(); 73 result = getLine(reader); 74 } 75 return result; 76 } 77 78 // Not terribly efficient, but does the job. 79 // Works for reading a line from stdin or a file. 80 // Returns null on EOF. If EOF appears in the middle 81 // of a line, returns that line, then null on next call. 82 // 83 public static String getLine(InputStreamReader reader) { 84 StringBuffer b = new StringBuffer(); 85 int c; 86 try { 87 while ((c = reader.read()) != -1 && c != '\n') { 88 if (c != '\r') 89 b.append((char)c); 90 } 91 } catch (IOException ioe) { 92 c = -1; 93 } 94 95 if (c == -1 && b.length() == 0) 96 return null; 97 else 98 return b.toString(); 99 } 100 101 public void run(boolean removeExistingDatabase, String databaseName) 102 throws DatabaseException, FileNotFoundException { 103 104 // Remove the previous database. 105 if (removeExistingDatabase) 106 new File(databaseName).delete(); 107 108 // Create the database object. 109 // There is no environment for this simple example. 110 DatabaseConfig dbConfig = new DatabaseConfig(); 111 dbConfig.setErrorStream(System.err); 112 dbConfig.setErrorPrefix("AccessExample"); 113 dbConfig.setType(DatabaseType.BTREE); 114 dbConfig.setAllowCreate(true); 115 Database table = new Database(databaseName, null, dbConfig); 116 117 // 118 // Insert records into the database, where the key is the user 119 // input and the data is the user input in reverse order. 120 // 121 InputStreamReader reader = new InputStreamReader(System.in); 122 123 for (;;) { 124 String line = askForLine(reader, System.out, "input> "); 125 if (line == null) 126 break; 127 128 String reversed = (new StringBuffer(line)).reverse().toString(); 129 130 // See definition of StringDbt below 131 // 132 StringEntry key = new StringEntry(line); 133 StringEntry data = new StringEntry(reversed); 134 135 try { 136 if (table.putNoOverwrite(null, key, data) == OperationStatus.KEYEXIST) 137 System.out.println("Key " + line + " already exists."); 138 } catch (DatabaseException dbe) { 139 System.out.println(dbe.toString()); 140 } 141 } 142 143 // Acquire an iterator for the table. 144 Cursor cursor; 145 cursor = table.openCursor(null, null); 146 147 // Walk through the table, printing the key/data pairs. 148 // See class StringDbt defined below. 149 // 150 StringEntry key = new StringEntry(); 151 StringEntry data = new StringEntry(); 152 while (cursor.getNext(key, data, null) == OperationStatus.SUCCESS) 153 System.out.println(key.getString() + " : " + data.getString()); 154 cursor.close(); 155 table.close(); 156 } 157 158 // Here's an example of how you can extend DatabaseEntry in a 159 // straightforward way to allow easy storage/retrieval of strings, 160 // or whatever kind of data you wish. We've declared it as a static 161 // inner class, but it need not be. 162 // 163 static /*inner*/ 164 class StringEntry extends DatabaseEntry { 165 StringEntry() { 166 } 167 168 StringEntry(String value) { 169 setString(value); 170 } 171 172 void setString(String value) { 173 byte[] data = value.getBytes(); 174 setData(data); 175 setSize(data.length); 176 } 177 178 String getString() { 179 return new String(getData(), getOffset(), getSize()); 180 } 181 } 182} 183