In this article, you will install and configure the backend of the database--web application to create a database schema and learn some simple tools to populate it with data. The database that you will be using is the Apache derby,100% pure Java™ relational database, which was originally developed under the name of Cloudscape™. Finally, IBM® acquired the Cloudscape code and then contributed its open-source version to the Apache project. The same project was issued by Sun Microsystems's javadb name, but there was no confusion between the two.
I chose Derby not because it has three names, but because it is lightweight and easy to configure. Unlike most relational databases, Derby can run in the same Java Virtual machine (JVM) as the Java-side server code. (If you like, you can also run it on a separate JVM.) This makes development and deployment easier, and Derby is a fast, ideal choice for small and medium Web applications.
Before you begin, there are a few caveats: first, to read this article, you should have a basic knowledge of relational databases, JDBC, and Structured Query Language (SQL). Second, for demonstration purposes, this article provides some content that may not be ideal in the production system in the code. I tried to point out those elements in the course of the story, but I'm not going to discuss performance tuning here.
Get Derby
Derby is provided as part of the Apache DB project. When writing this article, the latest version is the 10.1.3.1 version. If you are working in the Eclipse Integrated development Environment (IDE), it is sufficient to get the Derby_core_plugin and Derby_ui_plugin two plug-ins. If not, you can choose any other distribution that meets your needs. Some of these distributions contain only library files, some contain libraries and documents, some contain libraries with debug information, and distributions that have only source code. Derby is based on Java technology and can be run on any version 1.3 or later JVM. The code example in this article assumes that you are using Java 1.4.
Do not use Eclipse to set Derby
If you do not use Eclipse, unzip the downloaded release to any location you deem convenient. When you are done, make sure that the files Lib/derby.jar and Lib/derbytools.jar are located in the CLASSPATH variable. You can do this at the system level, which may help to set the environment variable Derby_install to the directory where Derby is located (including the Derby directory itself, in/opt/bin/db-derby-10.1.3.1-bin). You can also perform this action in the IDE or in the launcher script. If you need to use Derby in client/server mode and embedded mode, the file Lib/derbyclient.jar and Lib/derbynet.jar must also be in classpath.
Setting up Derby with Eclipse
If you are using Eclipse, it will be easier to make the setup work for your development. To set up Derby in Eclipse, complete the following steps:
Unzip the two plug-in files. Each plug-in file has a top-level directory named plugin.
Copy the contents of the directory to the Eclipse plug-in directory.
Open your project in Eclipse.
Click Project > Add Apache Derby Nature into Derby fantasy world. Doing so will add four library files to the project classpath and give you access to the IJ command line prompt.
Figure 1 shows the Derby menu after the derby nature has been added.
Figure 1. Eclipse Derby Menu
Even if you are using Eclipse for development, you must have a corresponding JAR file when you deploy your application. I will explain this topic in more detail in a later article.