Chapter 7. Databases
Prev | Part II. Programming with the Base API | Next

## Chapter 7. 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.

Also, note that in the previous section of this book, Programming with the Direct Persistence Layer, we described the DPL The DPL handles all database management for you, including creating all primary and secondary databases as is required by your application. That said, if you are using the DPL you can access the underlying database for a given index if necessary. See the Javadoc for the DPL for more information.

## 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
Part II. Programming with the Base API | Home | Closing Databases