In this JDBC tutorial, you will learn to get started with Apache Derby (JavaDB) by writing code to connect to a database.
You know, Apache Derby is a lightweight, portable database engine written purely in Java. Java DB is a just an Oracle’s distribution of Derby in their JDK. However, since Java 9, JavaDB is removed from JDK installation.
This article presents the steps to quickly get started with Derby, from downloading its JDBC driver to write code for making connections.
Table of content:
Download the latest version of Derby here (as of this writing, the latest release is 10.9.1.0). The distribution includes the following pieces of software component:
Component | Jar files |
Embedded database engine and JDBC driver | derby.jar |
Network client JDBC driver | derbyclient.jar |
Network server | derbynet.jar, derbyrun.jar |
Command line tools | derbytools.jar |
Localization messages | derbyLocale_xx_YY.jar |
If you are using JDK 1.7, then Derby is already included in JDK installation under the name Java DB in JDK_HOME\db directory. The jar files are located in JDK_HOME\db\lib directory. If you are using JDK 9 or newer, you need to download Apache Derby JAR files.
In both case, you have to place appropriate jar file to the classpath:
Derby differentiates two types of JDBC driver:
Type of driver | JDBC Driver Class Name |
Embedded driver | org.apache.derby.jdbc.EmbeddedDriver |
Network client driver | org.apache.derby.jdbc.ClientDriver |
So if you are planning to use everything of Derby in one machine, go with the embedded driver. Or if the JDBC client connects to Derby server on a remote machine, go with the network client driver.
If you are using Java 5.0 or earlier, you have to load the driver explicitly like this:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Or:
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
However, since Java 6.0 or later, loading JDBC driver as such becomes optional. The driver manager can load appropriate driver based on the database connection URL.
Following is the syntax of Derby JDBC database connection URL for the embedded driver:
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
Example connection URLs:
jdbc:derby:codejava/webdb;create=true
jdbc:derby:E:/projects/codejava/webdb;create=true
jdbc:derby:memory:codejava/webdb;create=true
jdbc:derby:classpath:webdb
Where the absolute directory E:/projects/codejava is added to the classpath.
jdbc:derby:jar:webdb
jdbc:derby:jar:(E:/projects/db.jar)webdb
jdbc:derby:;shutdown=true
Here is the syntax of Derby JDBC database connection URL for the network client driver:
jdbc:derby://server[:port]/databaseName[;attribute=value]*
The default port is 1527 if omitted. For example, to connect the user tom with password secret to the database webdb on the server dbserver, use the following URL:
jdbc:derby://dbserver/webdb;user=tom;password=secret
With JDBC, there are three different ways to establishing a connection to the database, corresponding to three version of the method getConnection() of the DriverManager class:
String dbURL = "jdbc:derby:codejava/webdb;create=true"; Connection conn = DriverManager.getConnection(dbURL);
String dbURL = "jdbc:derby://localhost/webdb;create=true"; String user = "tom"; String password = "secret"; Connection conn = DriverManager.getConnection(dbURL, user, password);
String dbURL = "jdbc:derby://localhost/webdb"; Properties properties = new Properties(); properties.put("create", "true"); properties.put("user", "tom"); properties.put("password", "secret"); Connection conn = DriverManager.getConnection(dbURL, properties);
And following is a full example program:
package net.codejava.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; /** * This program demonstrates how to connect to Apache Derby (Java DB) database * for the embedded driver and network client driver. * @author www.codejava.net * */ public class JdbcDerbyConnection { public static void main(String[] args) { try { // connect method #1 - embedded driver String dbURL1 = "jdbc:derby:codejava/webdb1;create=true"; Connection conn1 = DriverManager.getConnection(dbURL1); if (conn1 != null) { System.out.println("Connected to database #1"); } // connect method #2 - network client driver String dbURL2 = "jdbc:derby://localhost/webdb2;create=true"; String user = "tom"; String password = "secret"; Connection conn2 = DriverManager.getConnection(dbURL2, user, password); if (conn2 != null) { System.out.println("Connected to database #2"); } // connect method #3 - network client driver String dbURL3 = "jdbc:derby://localhost/webdb3"; Properties properties = new Properties(); properties.put("create", "true"); properties.put("user", "tom"); properties.put("password", "secret"); Connection conn3 = DriverManager.getConnection(dbURL3, properties); if (conn3 != null) { System.out.println("Connected to database #3"); } } catch (SQLException ex) { ex.printStackTrace(); } } }
That's Java code example to connect to Apache Derby database.