Using in-memory databases
Derby Developer's Guide
31
1. Use the encryptionKey attribute in the connection URL.
For example to create the database and encrypt the database
encDB
using an
external key, specify this URL:
jdbc:derby:encDB;create=true;dataEncryption=true;encryptionAlgorithm=DES/
CBC/NoPadding;encryptionKey=6162636465666768
Attention: If you lose the encryption key you will not be able to boot the database.
Booting an encrypted database
You must specify several attributes in the URL when you boot an encrypted database.
You must specify these attributes the first time that you connect to the database within a
JVM session, or after you shut the database down within the same JVM session.
To boot an existing encrypted database:
1. The attribute that you specify depends on how the database was originally
encrypted:
· If the database was encrypted using the bootPassword mechanism, specify
the bootPassword attribute. For example:
jdbc:derby:wombat;bootPassword=clo760uds2caPe
· If the database was encrypted using an external key, specify the
encryptionKey attribute. For example:
jdbc:derby:flintstone;encryptionAlgorithm=AES/CBC/NoPadding;
encryptionKey=c566bab9ee8b62a5ddb4d9229224c678
If the algorithm that was used when the database was created is not the
default algorithm, you must also specify the encryptionAlgorithm attribute. The
default encryption algorithm used by Derby is DES/CBC/NoPadding.
Specifying attributes in a properties object
Instead of specifying attributes on the connection URL, you can specify attributes
as properties in a
Properties
object that you pass as a second argument to the
DriverManager.getConnection
method.
For example, to set the user name and password:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Properties p = new Properties();
p.setProperty("user", "sa");
p.setProperty("password", "manager");
p.setProperty("create", "true");
Connection conn = DriverManager.getConnection(
"jdbc:derby:mynewDB", p);
If you are running on JDK 6 or higher, you do not normally need to invoke
Class.forName()
. In that environment, the
EmbeddedDriver
loads automatically.
The only exception to this rule is when you need to shut down Derby in the middle of your
application and then restart it. To restart Derby, create a new instance of the driver as
follows:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Using in-memory databases
For testing and developing applications, or for processing transient or reproducible data,
you can use Derby's in-memory database facility.