connection to Java DB always fails

The documentation for Java DB says that it comes bundled with the JDK, and I have read that no special class loading is required in later Java versions (I have Java 8). However, trying to connect to it fails completely:

DriverManager.getConnection("jdbc:derby:testDB;create=true")

This throws a SQLException, with the SQL state being 08001 (connection failed).

I also tried explicitly loading the driver class, but it also throws an error:

Class.forName("org.apache.derby.jdbc.ClientDriver");// ClassNotFoundException

The code below cannot even compile because the derby classes are not found:

DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());

How do I connect to a database with Java DB?

To use the “built-in” Java DB, you have to add the derby jar to your classpath manually. This is how I did it in Gradle:

testCompile files("$System.env.JAVA_HOME/db/lib/derby.jar")

I dislike this because it means that you are dependent on the local directory structure, as opposed to normal dependencies which are resolved by name and version number.

I had expected “Java DB” to be fully integrated into the standard library, but this is not the case. It is simply colocated with every JDK installation. As this has the additional drawback of generally being an outdated derby release, I will ignore Java DB and just use the latest derby version from maven central:

testCompile group: 'org.apache.derby', name: 'derby', version: '10.13.1.1'

Note also that the previously required Class.forName(…) statement still throws ClassNotFoundException; the statement is not required to make derby work.