Java Example Connect to Apache Derby (Java DB) with JDBC
- Details
- Written by Nam Ha Minh
- Last Updated on 04 June 2019   |   Print Email
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:
1. Downloading Derby JDBC driver library
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.jar: for embedded driver.
- derbyclient.jar: for network client driver.
2. Loading Derby JDBC drivers
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.
3. Derby JDBC database connection URL for embedded driver
Following is the syntax of Derby JDBC database connection URL for the embedded driver:
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
- Where subsubprotocol tells where Derby should look for the database. It can be one of:
- directory: looks for the database in the file system. The directory can be relative path or absolute path. For the relative path, Derby will look in the system directory (specified by the environment variable user.dir). This is the default location if subsubprotocol is not specified.
- memory: looks for the database in memory. This may e useful in case we only use a temporary database.
- classpath: looks for the database in the file system which is relative to the classpath directory. In this way the database is treated as in read-only mode.
- jar: looks for the database inside a jar or zip file. Read-only mode.
- attribute=value: specifies one or more additional attributes when making the connection. Some commonly used attributes are:
- create=true: creates the database if it does not exist.
- shutdown=true: closes the database. This must be used without database name.
- user=<username>: specifies the username to connect.
- password=<password>: specifies password of the username to connect.
Example connection URLs:
- Connect and create a database called webdb under the directory codejava. The database path is relative to the system directory.
jdbc:derby:codejava/webdb;create=true
- Connect to a database in the file system using absolute path:
- Connect to a database in the file system using absolute path:
jdbc:derby:E:/projects/codejava/webdb;create=true
- Connect and create a database if not exist in the memory:
jdbc:derby:memory:codejava/webdb;create=true
- Connect to a database presents in the classpath:
jdbc:derby:classpath:webdb
Where the absolute directory E:/projects/codejava is added to the classpath.
- Connect to a database called webdb inside a jar file which is on the classpath:
jdbc:derby:jar:webdb
- Connect to a database called webdb inside a jar file db.jarwhich is not on the classpath:
jdbc:derby:jar:(E:/projects/db.jar)webdb
- Shutdown the current database:
jdbc:derby:;shutdown=true
4. Derby JDBC database connection URL for network client driver
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
5. Making Derby JDBC connection examples
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:
- Using only a database URL:
String dbURL = "jdbc:derby:codejava/webdb;create=true"; Connection conn = DriverManager.getConnection(dbURL);
- Using a database URL with user and password:
String dbURL = "jdbc:derby://localhost/webdb;create=true"; String user = "tom"; String password = "secret"; Connection conn = DriverManager.getConnection(dbURL, user, password);
- Using a database URL with a Properties object:
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.
Other Apache Derby Tutorials:
- How to Get Started with Apache Derby (JavaDB)
- How to use Derby in embedded mode
- How to use Derby in network client-server mode
JDBC API References:
Related JDBC Tutorials:
- JDBC CRUD Tutorial
- JDBC Transaction Tutorial
- How to Read Database Meta Data in JDBC
- How to call stored procedure with JDBC
Comments
"It has found a network protocol error and the connection is complete : We have detected a syntax error in the data stream protocol name: . 0x9.236 Have you attempted to establish an unencrypted connection to a server . SSL ? "
Thank you
Check if you the Apache Derby JAR files are available under JDK path or in the classpath. Also check if you use this statement:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
java version "1.7.0_85"
OpenJDK Runtime Environment (IcedTea 2.6.1) (7u85-2.6.1-5ubuntu0.14.04.1)
OpenJDK Server VM (build 24.85-b03, mixed mode)
Check if you are loading your driver properly, that error means the driver is not being loaded - probably an incorrect url to load the driver