Apache Derby Tutorial PDF
Apache Derby Tutorial PDF
i
Apache Derby
Audience
This tutorial is prepared for beginners to help them understand the basic concepts related
to Apache Derby. This tutorial will give you enough understanding on the various SQL
queries of Apache along with JDBC examples.
Prerequisites
Before you start practicing with various types of examples given in this tutorial, I am
assuming that you are already aware about what a database is, especially the RDBMS and
what is a computer programming language.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
Apache Derby
Table of Contents
About the Tutorial ...................................................................................................................................... i
Audience .................................................................................................................................................... i
Prerequisites .............................................................................................................................................. i
ij tool ....................................................................................................................................................... 12
ii
Apache Derby
Creating an Index..................................................................................................................................... 70
iii
Apache Derby
Dropping Indexes..................................................................................................................................... 72
Dropping a procedure.............................................................................................................................. 78
Dropping a Schema.................................................................................................................................. 80
iv
1. Apache Derby – Introduction Apache Derby
Oracle released the equivalent of Apache Derby with the name JavaDB.
Platform independent: Derby uses on-disc database format where the databases
in it are stored in a file in the disc within the directory with the same name as the
database.
No modifying data: Because of this, you can move derby databases to other
machines without modifying the data.
Less space: Derby database has a small footprint, i.e., it occupies less space and
it is easy to use and deploy it.
Derby does not support indexes for datatypes such as BLOB and LONGVARCHAR.
If Derby does not have enough disc space, it will shut down immediately.
Data storage
While storing data, Apache Derby follows a concept known as conglomerate. In this, data
of a table will be stored in a separate file. In the same way, each index of a table is also
stored in a separate file. Thus, there will be a separate file for every table or index in the
database.
1
Apache Derby
derbytools.jar Command line This jar file holds tools such as sysinfo,
tools ij, and dblook.
2
2. Apache Derby – Deployment Modes Apache Derby
You can deploy apache derby in two modes, namely embedded mode and server mode.
Embedded mode
You can run derby in embedded mode using Java application (using embedded driver). If
you deploy Derby in embedded mode, the database engine will run in the same JVM as
the Java application. It starts and stops with the application. You can access the database
only with this application.
Server mode
In the server mode, derby will be run in the JVM of an application server where you can
send a request to the server to access it. Unlike in embedded mode, multiple applications
(java) can send a request to the server and access the database.
3
3. Apache Derby – Environment Setup Apache Derby
Select and click on the link of the latest version of Apache Derby.
4
Apache Derby
On clicking the selected link, you will be redirected to the Distributions page of apache
derby. If you observe here, derby provides distributions namely, db-derby-bin, db-derby-
lib.zip, db-derby-lib-debug.zip, and db-derby-src.zip.
Download the db-derby-bin folder. Copy its contents to a separate folder where you
wanted to install Apache Derby. (for example, say C:\Derby)
Make sure that you already have set the JAVA_HOME variable by passing the
location of bin folder of Java Installation folder, and include the JAVA_HOME/bin
in the PATH variable.
Embedded mode: In this, you need to access the database using Embedded Derby
JDBC driver. You can start and stop derby through Java application. Both Database
engine and your application will run on the same JVM.
Network Server mode: In this mode, you can access Derby in a typical client-
server fashion, where Derby is embedded in the server system. Then, the client
machines running in different JVM’s (that of the Server) will send requests to the
server, and the server responds to those requests.
5
Apache Derby
The client can be another JVM in the same system machine of the server or a Java
application from a remote system.
Or, you can set the classpath for required jar files by running the setEmbeddedCP
command. Browse through the bin directory of Apache Derby and run this file as shown
below:
C:\Users\MYUSER>cd %DERBY_HOME%/bin
C:\Derby\bin>setEmbeddedCP.bat
C:\Derby\bin>SET DERBY_HOME=C:\Derby
C:\Derby\bin>set
CLASSPATH=C:\Derby\lib\derby.jar;C:\Derby\lib\derbytools.jar;C:\Derby/lib/derby
optionaltools.jar;C:\Users\Tutorialspoint\Google
Drive\Office\Derby\derby_zip\New folder\db-derby-10.12.1.1-
bin\lib;C:\EXAMPLES_\Task\jars\*;C:\EXAMPLES\jars\mysql-connector-java-5.1.40-
bin.jar;C:\Users\Tutorialspoint\Google Drive\Office\37.Junit
Update\jars;C:\Program Files\Apache Software Foundation\Tomcat
8.5\lib\*;C:\Derby\lib\*;
After setting up Apache Derby, to access it, run Java programs using the embedded driver.
Verification
You can verify the setup using the ij tool as shown below:
C:\Derby\bin>ij
ij version 10.14
ij> connect 'jdbc:derby:SampleDB;create=true';
ij>
Or, you can set the class path for required jar files by running the setNetworkServerCP
command. Browse through the bin directory of Apache Derby and run this file as shown
below:
C:\Users\MYUSER>cd %DERBY_HOME%/bin
C:\Derby\bin>setNetworkServerCP.bat
C:\Derby\bin>SET DERBY_INSTALL=C:\Derby
6
Apache Derby
C:\Derby\bin>set
CLASSPATH=C:\Derby\lib\derbynet.jar;C:\Derby\lib\derbytools.jar;C:\Derby/lib/de
rbyoptionaltools.jar;C:\Users\Tutorialspoint\Google
Drive\Office\Derby\derby_zip\New folder\db-derby-10.12.1.1-
bin\lib;C:\EXAMPLES_\Task\jars\*;C:\EXAMPLES\jars\mysql-connector-java-5.1.40-
bin.jar;C:\Users\Tutorialspoint\Google Drive\Office\37.Junit
Update\jars;C:\Program Files\Apache Software Foundation\Tomcat
8.5\lib\*;C:\Derby\lib\*;
C:\Derby\bin>startNetworkServer
Fri Jan 04 11:20:30 IST 2019 : Security manager installed using the Basic
server security policy.
Fri Jan 04 11:20:30 IST 2019 : Apache Derby Network Server - 10.14.2.0 -
(1828579) started and ready to accept connections on port 1527
Or, you can start the server using derbyrun.jar as shown below:
C:\Users\MYUSER>cd %DERBY_HOME%/lib
C:\Derby\lib>java -jar derbyrun.jar server start
Fri Jan 04 11:27:20 IST 2019: Security manager installed using the Basic server
security policy.
Fri Jan 04 11:27:21 IST 2019: Apache Derby Network Server - 10.14.2.0 -
(1828579) started and ready to accept connections on port 1527
Network Client
In client, add the jar files derbyclient.jar and derbytools.jar to the CLASSPATH. Or,
run the setNetworkClientCP command as shown below:
C:\Users\MYUSER>cd %DERBY_HOME%/bin
C:\Derby\bin>setNetworkClientCP
C:\Derby\bin>SET DERBY_HOME=C:\Derby
C:\Derby\bin>set
CLASSPATH=C:\Derby\lib\derbyclient.jar;C:\Derby\lib\derbytools.jar;C:\Derby/lib
/derbyoptionaltools.jar;C:\Derby\lib\derby.jar;C:\Derby\lib\derbytools.jar;C:\D
erby/lib/derbyoptionaltools.jar;C:\Users\Tutorialspoint\Google
Drive\Office\Derby\derby_zip\New folder\db-derby-10.12.1.1-
bin\lib;C:\EXAMPLES_\Task\jars\*;C:\EXAMPLES\jars\mysql-connector-java-5.1.40-
bin.jar;C:\Users\Tutorialspoint\Google Drive\Office\37.Junit
Update\jars;C:\Program Files\Apache Software Foundation\Tomcat
8.5\lib\*;C:\Derby\lib\*;
7
Apache Derby
Then from this client, you can send requests to the server.
Verification
You can verify the setup using the ij tool as shown below:
C:\Derby\bin>ij
ij version 10.14
ij> connect 'jdbc:derby://localhost:1527/SampleDB;create=true';
ij>
8
Apache Derby
In the Java Build Path frame in the Libraries tab, click on Add External JARs.
And select the required jar files in the lib folder of the Derby installation folder and click
on Apply and Close.
9
Apache Derby
10