Administrative Methods
Prev | Chapter 7. Databases | Next

## Administrative Methods

Both the `Environment` and `Database` classes provide methods that are useful for manipulating databases. These methods are:

- `Database.getDatabaseName()`

  Returns the database's name.

      String dbName = myDatabase.getDatabaseName();

- `Database.getEnvironment()`

  Returns the `Environment` that contains this database.

      Environment theEnv = myDatabase.getEnvironment();

- `Database.preload()`

  Preloads the database into the in-memory cache. Optionally takes a `long` that identifies the maximum number of bytes to load into the cache. If this parameter is not supplied, the maximum memory usage allowed by the evictor thread is used.

      myDatabase.preload(1048576l); // 1024*1024

- `Environment.getDatabaseNames()`

  Returns a list of Strings of all the databases contained by the environment.

      import java.util.List;
      ...
      List myDbNames = myDbEnv.getDatabaseNames();
      for(int i=0; i < myDbNames.size(); i++) {
          System.out.println("Database Name: " + (String)myDbNames.get(i));
      }

- `Environment.removeDatabase()`

  Deletes the database. The database must be closed when you perform this action on it.

      String dbName = myDatabase.getDatabaseName();
      myDatabase.close();
      myDbEnv.removeDatabase(null, dbName);

- `Environment.renameDatabase()`

  Renames the database. The database must be closed when you perform this action on it.

      String oldName = myDatabase.getDatabaseName();
      String newName = new String(oldName + ".new", "UTF-8");
      myDatabase.close();
      myDbEnv.renameDatabase(null, oldName, newName);

- `Environment.truncateDatabase()`

  Deletes every record in the database and optionally returns the number of records that were deleted. Note that it is much less expensive to truncate a database without counting the number of records deleted than it is to truncate and count.

      int numDiscarded =
          myEnv.truncate(null,                          // txn handle
                         myDatabase.getDatabaseName(),  // database name
                         true);                         // If true, then the
                                                        // number of records
                                                        // deleted are counted.
      System.out.println("Discarded " + numDiscarded +
                         " records from database " +
                         myDatabase.getDatabaseName());

Prev | Up | Next
Database Properties | Home | Database Example