Apache Derby: A Lightweight Java-Based Relational Database
Apache Derby is a full-featured, open-source relational database management system (RDBMS) implemented entirely in Java. Small in footprint yet standards-compliant, it's an excellent choice for embedded applications, prototyping, and testing environments. This post explores what makes Derby useful and how to get started.
What Is Apache Derby?
Derby is developed and maintained by the Apache Software Foundation. Oracle also distributes it under the name Java DB as part of some JDK builds. It supports the SQL standard, JDBC, and Java stored procedures, all while occupying roughly 3.5 MB for the base engine.
Key characteristics include:
- Pure Java implementation — runs anywhere a JVM runs.
- Small footprint — ideal for embedding directly inside applications.
- Standards-based — supports SQL, JDBC, and Java EE.
- Zero administration — no complex setup or DBA required.
Deployment Modes
Derby supports two primary deployment modes.
1. Embedded Mode
The database engine runs in the same JVM as your application. There is no separate server process, making it perfect for desktop apps and unit tests.
// Embedded connection URL
String url = "jdbc:derby:myDB;create=true";
Connection conn = DriverManager.getConnection(url);
2. Client/Server Mode
Derby runs as a network server (using the Derby Network Server), allowing multiple clients to connect over TCP/IP.
// Client/server connection URL
String url = "jdbc:derby://localhost:1527/myDB;create=true";
Connection conn = DriverManager.getConnection(url);
Getting Started
Adding the Dependency
For a Maven project, include the appropriate artifact.
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.17.1.0</version>
</dependency>
Note: From Derby 10.15 onward, the driver class is
org.apache.derby.jdbc.EmbeddedDriver. Modern JDBC uses auto-loading via the service provider mechanism, so explicit class loading is usually unnecessary.
Creating a Table and Inserting Data
import java.sql.*;
public class DerbyExample {
public static void main(String[] args) throws SQLException {
String url = "jdbc:derby:sampleDB;create=true";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// Create a table
stmt.execute("CREATE TABLE employees (" +
"id INT PRIMARY KEY, " +
"name VARCHAR(100), " +
"salary DECIMAL(10,2))");
// Insert a row
stmt.executeUpdate(
"INSERT INTO employees VALUES (1, 'Alice', 75000.00)");
// Query the data
ResultSet rs = stmt.executeQuery(
"SELECT * FROM employees");
while (rs.next()) {
System.out.printf("%d - %s - %.2f%n",
rs.getInt("id"),
rs.getString("name"),
rs.getDouble("salary"));
}
}
}
}
Shutting Down Cleanly
In embedded mode, shutting down Derby properly flushes data and releases file locks.
try {
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException e) {
// Derby throws SQLState XJ015 on successful shutdown — this is expected
if (!"XJ015".equals(e.getSQLState())) {
throw e;
}
}
Common Use Cases
| Use Case | Why Derby Fits |
|---|---|
| Unit and integration testing | Fast, in-memory or file-based, no external server |
| Desktop applications | Embedded engine ships with the app |
| Prototyping | Zero configuration and quick setup |
| Learning SQL/JDBC | Lightweight and easy to reset |
In-Memory Databases
For tests, Derby can run entirely in memory, discarding data when the JVM stops.
String url = "jdbc:derby:memory:testDB;create=true";
Advantages and Limitations
Advantages:
- Trivial to embed and distribute.
- No installation or administration overhead.
- Strong SQL standard compliance.
- Portable across platforms.
Limitations:
- Not designed for very large datasets or heavy concurrency.
- Fewer advanced features than PostgreSQL or Oracle.
- Performance under high load lags behind dedicated servers.
Conclusion
Apache Derby fills an important niche in the Java ecosystem: a compact, standards-compliant database that requires virtually no setup. While it isn't intended to replace enterprise-grade servers for large production workloads, it excels in embedded scenarios, automated testing, and rapid prototyping. If your application needs a self-contained SQL database without operational overhead, Derby is well worth considering.
Top comments (0)