Apache Derby 简明教程

Apache Derby - Environment Setup

后续章节将说明如何下载和安装 Apache Derby。

Downloading Apache Derby

访问 Apache Derby 主页 https://db.apache.org/derby/ 。单击“下载”选项卡。

home page of apache derby

选择并单击 Apache Derby 最新版本的链接。

latest version of apache derby

单击所选链接后,您将被重新定向到 Apache Derby 的 Distributions 页面。如果您在此观察,derby 提供了名为 db-derby-bin、db-derbylib.zip、db-derby-lib-debug.zip 和 db-derby-src.zip 的发行版。

下载 db-derby-bin 文件夹。将其内容复制到您要安装 Apache Derby 的单独文件夹中。(例如, C:\Derby

现在,要使用 Derby,

  1. 确保您已经通过传递 Java 安装文件夹中 bin 文件夹的位置设置了 JAVA_HOME 变量,并将 JAVA_HOME/bin 包含在 PATH 变量中。

  2. 创建一个新的环境变量 DERBY_HOME ,其值为 C:\Derby。

  3. db-derby-bin 发行版的 bin 文件夹(我们将其更改为 C:\Derby\bin)包含所有必需的 jar 文件。

如前所述,Apache Derby 可以通过以下两种方式安装/部署 −

  1. Embedded mode − 在此模式下,您需要使用嵌入式 Derby JDBC 驱动程序访问数据库。您可以通过 Java 应用程序启动和停止 derby。数据库引擎和您的应用程序都将在同一 JVM 中运行。

  2. Network Server mode − 在此模式下,您可以以典型的客户端服务器方式访问 Derby,其中 Derby 嵌入在服务器系统中。然后,在不同 JVM(服务器的 JVM)中运行的客户端计算机将向服务器发送请求,服务器对这些请求做出响应。

客户端可以是服务器相同系统计算机中的另一个 JVM 或远程系统的 Java 应用程序。

Installing Derby in Embedded Mode

To install Apache Derby in embedded mode, include the jar file derby.jar in your CLASSPATH.

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>

Installing Derby in Network Server Mode

To install Apache Derby in network server mode, you need to include derbynet.jar and derbytools.jar files to the CLASSPATH.

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
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\*;

Starting Derby in Server Mode

You can start Network Server by running the command startNetworkServer. Browse through the bin directory of Apache Derby and run this command as shown below −

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\*;

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>

Apache Derby Eclipse Environment

While working with Eclipse, you need to set the build path for all the required jar files.

Step 1: Create a project and set build path

Open eclipse and create a sample project. Right click on the project and select the option Build Path → Configure Build Path as shown below −

configure build path

Libraries 选项卡的 Java Build Path 框架中,单击 Add External JARs

java build path

And select the required jar files in the lib folder of the Derby installation folder and click on Apply and Close.