Setting up Apache Derby, a lightweight, embedded SQL database written entirely in Java, can be segregated into a few straightforward steps. Derby can operate in two primary modes: embedded and server. In the embedded mode, Derby runs within a Java application and is visible to that application alone. In server mode, Derby acts as a network database server, accessible by multiple clients over a network.
I’ll guide you through setting up Derby in both embedded and server modes, assuming you are starting from scratch.
Prerequisites
- Java JDK installed on your system. Apache Derby requires Java to run.
Downloading Apache Derby
- Visit the official Apache Derby website: Apache Derby Downloads.
- Download the latest stable release. As of my last knowledge update, Derby comes in two packages: - bin distribution (includes documentation, demo programs, and the Derby Network Server) - lib distribution (the core Derby libraries only).
- Extract the downloaded archive to a directory of your choice, which we'll refer to as
<DERBY_HOME>.
Setting Up Environment Variables (Optional but Recommended)
-
Set DERBY_HOME:
- On Windows: Set
DERBY_HOME to the path where you extracted Derby by going into System Properties -> Environment Variables, and adding a new system variable named DERBY_HOME that points to <DERBY_HOME>.
- On Unix/Linux/MacOS: Add
export DERBY_HOME=<path_to_derby> to your .bashrc, .bash_profile, or .profile file.
-
Update the PATH variable to include Derby's bin directory:
- On Windows: Add
;%DERBY_HOME%\bin at the end of the existing Path variable.
- On Unix/Linux/MacOS: Add
export PATH=$PATH:$DERBY_HOME/bin to the same file you updated in the previous step.
Embedded Mode Setup
In embedded mode, you simply include Derby's library in your Java project's classpath.
- For a Java project without build tools, copy
derby.jar from the <DERBY_HOME>/lib directory into your project library path or reference it directly.
- For Maven projects, include Derby as a dependency in your
pom.xml:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version> <!-- Use the latest version -->
</dependency>
- For Gradle projects, add Derby to your
build.gradle:
dependencies {
implementation 'org.apache.derby:derby:10.15.2.0' // Use the latest version
}
Server Mode Setup
-
Start the Derby Network Server:
- Open a terminal or command prompt.
- Navigate to
<DERBY_HOME>/bin.
- Execute
startNetworkServer (or startNetworkServer.bat on Windows). This starts Derby in server mode, listening on port 1527 by default.
-
Connecting to the Database:
- To connect to the network server, you can use the
ij tool that comes with Derby for a CLI, or connect using any standard JDBC database tools or libraries from your Java application.
- JDBC connection URL for a network server usually looks like:
jdbc:derby://localhost:1527/myDb;create=true, where myDb is your database name. The create=true attribute indicates that Derby should create the database if it does not exist.
-
Stop the Derby Network Server:
- You can stop the server by executing
stopNetworkServer (or stopNetworkServer.bat on Windows) in <DERBY_HOME>/bin, or by pressing Ctrl+C in the terminal where the server was started.
Conclusion
That's a basic setup for Apache Derby in both embedded and server mode. Remember, whenever utilizing Derby, ensure your Java application’s compatibility with the Java version required by the Derby version you're using. For development, it's generally easier to start with embedded mode, but server mode offers more flexibility for client-server applications.