2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2001
5
* Sleepycat Software. All rights reserved.
7
* $Id: AccessExample.java,v 11.7 2001/05/12 21:43:27 dda Exp $
10
package com.sleepycat.examples;
12
import com.sleepycat.db.*;
14
import java.io.FileNotFoundException;
15
import java.io.InputStreamReader;
16
import java.io.IOException;
17
import java.io.PrintStream;
21
private static final String FileName = "access.db";
23
public AccessExample()
27
public static void main(String argv[])
31
AccessExample app = new AccessExample();
34
catch (DbException dbe)
36
System.err.println("AccessExample: " + dbe.toString());
39
catch (FileNotFoundException fnfe)
41
System.err.println("AccessExample: " + fnfe.toString());
47
// Prompts for a line, and keeps prompting until a non blank
48
// line is returned. Returns null on error.
50
static public String askForLine(InputStreamReader reader,
51
PrintStream out, String prompt)
54
while (result != null && result.length() == 0) {
57
result = getLine(reader);
62
// Not terribly efficient, but does the job.
63
// Works for reading a line from stdin or a file.
64
// Returns null on EOF. If EOF appears in the middle
65
// of a line, returns that line, then null on next call.
67
static public String getLine(InputStreamReader reader)
69
StringBuffer b = new StringBuffer();
72
while ((c = reader.read()) != -1 && c != '\n') {
77
catch (IOException ioe) {
81
if (c == -1 && b.length() == 0)
88
throws DbException, FileNotFoundException
90
// Remove the previous database.
91
new File(FileName).delete();
93
// Create the database object.
94
// There is no environment for this simple example.
95
Db table = new Db(null, 0);
96
table.set_error_stream(System.err);
97
table.set_errpfx("AccessExample");
98
table.open(FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
101
// Insert records into the database, where the key is the user
102
// input and the data is the user input in reverse order.
104
InputStreamReader reader = new InputStreamReader(System.in);
107
String line = askForLine(reader, System.out, "input> ");
111
String reversed = (new StringBuffer(line)).reverse().toString();
113
// See definition of StringDbt below
115
StringDbt key = new StringDbt(line);
116
StringDbt data = new StringDbt(reversed);
121
if ((err = table.put(null,
122
key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
123
System.out.println("Key " + line + " already exists.");
126
catch (DbException dbe)
128
System.out.println(dbe.toString());
130
System.out.println("");
133
// Acquire an iterator for the table.
135
iterator = table.cursor(null, 0);
137
// Walk through the table, printing the key/data pairs.
138
// See class StringDbt defined below.
140
StringDbt key = new StringDbt();
141
StringDbt data = new StringDbt();
142
while (iterator.get(key, data, Db.DB_NEXT) == 0)
144
System.out.println(key.getString() + " : " + data.getString());
150
// Here's an example of how you can extend a Dbt in a straightforward
151
// way to allow easy storage/retrieval of strings, or whatever
152
// kind of data you wish. We've declared it as a static inner
153
// class, but it need not be.
156
class StringDbt extends Dbt
160
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
163
StringDbt(String value)
166
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
169
void setString(String value)
171
set_data(value.getBytes());
172
set_size(value.length());
177
return new String(get_data(), 0, get_size());