Chapter 2. Databases
Prev |  | Next

## Chapter 2. Databases

In Berkeley DB, a database is a collection of records. Records, in turn, consist of key/data pairings.

Conceptually, you can think of a `Database` as containing a two-column table where column 1 contains a key and column 2 contains data. Both the key and the data are managed using `DatabaseEntry` class instances (see Database Records for details on this class ). So, fundamentally, using a DB `Database` involves putting, getting, and deleting database records, which in turns involves efficiently managing information encapsulated by `DatabaseEntry` objects. The next several chapters of this book are dedicated to those activities.

## Opening Databases

You open a database by instantiating a `Database` object.

Note that by default, DB does not create databases if they do not already exist. To override this behavior, set the creation property to true.

The following code fragment illustrates a database open:

    package db.GettingStarted;

    import com.sleepycat.db.DatabaseException;
    import com.sleepycat.db.Database;
    import com.sleepycat.db.DatabaseConfig;

    import java.io.FileNotFoundException;
    ...

    Database myDatabase = null;

    ...

    try {
        // Open the database. Create it if it does not already exist.
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        myDatabase = new Database ("sampleDatabase.db",
                                   null,
                                   dbConfig);
    } catch (DatabaseException dbe) {
        // Exception handling goes here
    } catch (FileNotFoundException fnfe) {
        // Exception handling goes here
    }

Prev | Up | Next
Getting and Using DB | Home | Closing Databases